1

I am beginner to python programming, and I necessary to know what does this line means. I knew the basic syntax of for loop in python, but this line I have no idea, I am seeking for some professional in python to help me. I have spent a number of time to understand it :(

for prop, value in ((p, v) for p, v in properties if v is not None):
Jackdon Chew
  • 107
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [What does "list comprehension" mean? How does it work and how can I use it?](http://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it) – McGrady Apr 05 '17 at 03:29
  • It's a for loop... How about you run each piece independently? – OneCricketeer Apr 05 '17 at 03:29
  • It's a python script inside an image processing library script... I wish to understanding what does the script actual doing... Is it combine 2 for loop with properties? – Jackdon Chew Apr 05 '17 at 03:34

4 Answers4

2

Well, that is certainly a one-liner:

  1. for prop, value in This is a standard loop that will iterate over an iterable, like list, dictionary (keys), or tuple
  2. ((p, v) for p, v in properties if v is not None): This uses in an inline for loop to filter out None values. You can see that only items that pass the if are kept. And then we are not modifying the element at all ((p, v) for p, v in properties. We are just removing items where the value is None.
Neil
  • 13,042
  • 2
  • 26
  • 48
2

((p, v) for p, v in properties if v is not None)

This is a generator expression. It produces a generator object which emits a pair (p,v) from the list properties each time it is polled, skipping those pairs where v is None, until it runs out of properties. That is, it emits each of the property, value pairs for which some value is set. "Each time it is polled" just means "each time someone asks for the next() object"

for prop, value in some_collection:

This repeatedly asks some_collection for its next object, which had better be a pair. The elements of that pair are assigned to the values prop and value respectively and the body of the loop is executed.

Putting them together, you can see that we are looping over the non-null properties and taking some action with them (we don't know what action, since we don't have the loop body)

I would suggest you start by looking into list comprehensions and dict comprehensions, and then proceed to understand generators, since list and dict comps are conceptually simpler.

Jon Kiparsky
  • 6,854
  • 2
  • 20
  • 37
1

Its a list comprehension. For this particular example which you have mentioned it can be break down as following:

properties = {'prop1': 1, 'prop2':2, 'prop':3, 'prop4':None}
for p, v in properties.iteritems():
    if v is not None:
        print p, v

Output:

prop1 1
prop2 2
prop 3
Rahul Telgote
  • 134
  • 1
  • 7
0

((p, v) for p, v in properties if v is not None) is a generator expression.

Let's suppose properties is a list with a tuple (a,b) for simplicity's sake. It won't change much

What it's doing is:

  1. Consider the tuple (p, v) where p and v are found in properties[0] in that order.
  2. If v (found in properties[0]) is not None, yield it.
  3. Repeat with properties[1],properties[2], etc... until properties[-1].

What I mean with 'yield' is that these can actually be translated into functions, functions that use the keyword yield. I will not explain that but rather give you the next links.


Further reading:

Graham
  • 6,577
  • 17
  • 55
  • 76
Juan T
  • 1,163
  • 1
  • 8
  • 20