How to collect the value corresponding to a field of an array/list of named tuples
Question: I am new to Python, in the process of migrating from MATLAB. I was using the
`namedtuple` class the following way.

    
    
    from collections import namedtuple
    import numpy as np
    MyStruct = namedtuple("MyStruct", "field1 field2 field3")
    d = []
    d.append(MyStruct(field1=np.array(3,'int'),field2=np.array(4,'int'),field3=np.array(5,'int')))
    d
    d.append(MyStruct(field1=np.array(4,'int'),field2=np.array(5,'int'),field3=np.array(6,'int')))
    

Here `d` is a 2-element list of `namedtuple` objects. What I want is to obtain
an array of all `field1` values through all the elements of `d` in a nifty way
without the need to iterate through the elements of `d`. Something similar to
MATLAB, where we put square brackets around an array of structures.

I'd appreciate any insights into this matter.

Srini


Answer: This is what list comprehensions are for:

    
    
    field1vals = [x.field1 for x in d]
    

