1

I currently have this multi-dimensional list and I am trying to extract the first column from it:

>>> print(data)
[['com.borderstylo.retrollect', '0', '0'], ['aabasoft.presents.goldprice', '0', '0'], ['aberl.vlc.light.mote', '0', '0']]

when I use

sitelist = [] 
for row in data:
    sitelist.append(row[0])
print(sitelist)

I get the output below, which is what i am after.

['com.borderstylo.retrollect', 'aabasoft.presents.goldprice', 'aberl.vlc.light.mote']

However, when i use

sitelist = []
sitelist = (row[0] for row in data)
print(sitelist)
type(sitelist)

I get the output below instead, which is a generator object. What is a generator object and why does the 1st set of code return something different from the 2nd? both appear very similar.

<generator object <genexpr> at 0x001A9E10>
generator

Many thanks for replies. Newbie at python but really wanting to learn.

2 Answers2

1

List comprehensions in python are created using []. As an alternative, when dealing with very large data, you can construct a generator using the same syntax as with comprehensions, only with ().

Thus, just change () with [].

Pablo Arias
  • 360
  • 1
  • 9
1

row[0] for row in data gives a generator object. This is like lazy evaluation. It means this for loop does not get executed to produce the result, instead a generator object is created which can be used to execute the for loop one iteration at a time when and if required.
This is done for efficiency in terms of time and memory.

When this is enclosed in parenthesis, sitelist = (row[0] for row in data) is treated as in expression and the resulting generator object is stored (referenced) in sitelist.

When this is enclosed in square brackets, sitelist = [row[0] for row in data] it is a list comprehension. The for loop is executed right away and a list is constructed and stored (referenced) in sitelist.

Look at this answer for more information on iteration protocol in python.
Then explore generators in python. There many articles/blog posts which explain generator with varying level of details.

Community
  • 1
  • 1
shanmuga
  • 4,329
  • 2
  • 21
  • 35