Erik wrote, on January 09, 2017 8:06 PM > > On 10/01/17 03:02, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > >> IIRC, you create it using a list comprehension which creates the > >> records. A list comprehension always creates a list. > > > > Well no. The list is created with: > > > > records.extend(Record._make(row) for row in rows) > > No, the list is _extended_ by that code. The list is _created_ with a > line that will say something like "records = []" or "records > = list()" > (or "records = <expression that yields a list>").
The list was created with this code: infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv") rows = csv.reader(infile) fieldnames = next(rows) Record = namedtuple("Record", fieldnames) records = [Record._make(fieldnames)] records.extend(Record._make(row) for row in rows) I just pulled out the .extend statement to show you because that's what looks like a list comprehension, but turns out not to be one. We still get a list though, on that we agree. ;) > It's nice to see you agree that it's a list though. Oh, hold on ... ;) > > > I'm not exactly > > sure if this statement is a list comprehension. > > No, it's not. I was remembering an old message where someone > suggested > using the _make() method and that was expressed as a list > comprehension. > > What you have there is a call to list.extend() passing a _generator_ > comprehension as its only parameter (which in this case you > can consider > to be equivalent to a list comprehension as all of the data are > generated greedily). You see that I said "list.extend()". > That's because > 'records' is a list. > > >>>> type(records) > > <class 'list'> > > Yes, it's an instance of the list class. A list object. A list. > > >>> type(list()) > <class 'list'> > >>> type([]) > <class 'list'> > >>> class foo: pass > ... > >>> type(foo()) > <class '__main__.foo'> > >>> > > ... type() will tell you what class your object is an instance of. > "<class 'list'>" tells you that your object is a list. > > > And it behaves like a list sometimes, but many times > > not. > > I think that's impossible. I'm 100% sure it's a list. Please give an > example of when 'records' does not behave like a list. I gave an example in one of my last two replies to other people. The thing is that it's a list, but it's not a list of lists. It's a list of namedtuples, and the non-listlike behaviors appear when I'm directly working with the namedtuples. > > The only thing I don't think you have 100% correct is your assertion > > that records is a list. > > It's a list. I agree, now. > > But that's just a quibble. The important thing in this context is that > > both .sort() and sorted() treat it like a list and DTRT. > > That's because it's a list :) > > E. It is! A list of namedtuples that is, not a list of lists. sorted() and .sort work because they only interact with the outer data structure, which is a list. -- https://mail.python.org/mailman/listinfo/python-list