How does this for loop work?

Question:

I am trying to learn python and I am going through the book programming python. I know java pretty well so I decided to give python a try as well. I am going through an example using loops and I am confused about what is happening in this code

for person in people:
  for (name, value) in person:
    if name == 'name': print(value)

I know that there are two loops and person is incremented by 1 each time it loops through, what i dont understand is what is happening to the (name, value) in the second loop.
Could someone please explain to me what is happening?

Asked By: Will Jamieson

||

Answers:

Assuming person is a list of tuples/lists it is extracting the first and second value.

It is basically the same as:

for x in person:
    name = x[0]
    value = x[1]
    # or
    name, value = x
Answered By: Matt

Most likely it’s supposed to deal with three dimensional list of the following format:

people = [
    [['name', 'John'], ['age', 21]],
    [['name', 'Ann'],  ['age', 45]],
    [['name', 'Tom'],  ['age', 32]],
]

for person in people:
  # person is like [['name', 'Ann'],  ['age', 45]] here
  for (name, value) in person:
    # unpack [field_name, field_value] to name = field_name, value = field_value
    # so name is like 'name' (or 'age') and value is like 'Ann' (or 45)
    if name == 'name': print(value)

Information for every person is stored in a list of pairs. Iterating over this person list in that manner unpacks every pair into two separate variables: name and value that are later used to check field type and print value if it’s name field.

In the result this snippet will print names of all people.

Answered By: KL-7

A for loop has the form:

for vars in iterable:

and vars can be anything that can appear on the left-hand side of an assignment statement. Then each value from the iterable is assigned to that left-hand side.

In Python, you can assign to a number of names at once, in which case the right-hand side must be a list or tuple with that many values:

a, b, c = 1, 2, 3

So in your for loop, each value from person must be a tuple or list with two elements, a pair. Each iteration of the loop, the next pair is unpacked into name and value.

Answered By: Ned Batchelder

No, person is not incremented by 1 each time through the loop. It’s not a number. person receives the next item from people each time through the loop.

The second loop is the same: person is a list (or other sequence) and each iteration through the list retrieves one item from the list.

Each item in person is a key-value pair, represented as a two-item list (or other sequence; a tuple is likely) with the key first and the value second. Rather than iterating over this list, since we know there are two items, we can unpack the items to the variables name and value (a common Python idiom).

If we find the key-value pair that begins with “name”, then we have found the name of the person, so we print it.

This is a little confusing because the word “name” is used for the key of a key-value pair as well as for the key we’re looking for. If I were writing this, I would use “key” for the loop variable instead.

In short, people is a list of lists. Each list in people represents a person, and is itself a list of key-value pairs. Each key-value pair is a list of two items. The outer loop iterates over people, the inner loop iterates over the key-value pairs in the person.

Answered By: kindall

In short, “name” and “value” get assigned values from “person” for each element “person” in “people”.

Let’s back up a little bit.

for person in people:

iterates through the elements of a list named “people”. Each pass through this loop, the variable named “person” gets assigned the next element from the list named “people”.

for (name, value) in person:

The variable named “person” is a list in which each element is a tuple with two variables named “name” and “value”. Each pass through this loop, the current “person” list element gets split up into its constituent parts, “name” and “people”.

So “people” looks like

[person1, person2, person3, ...]

and each “person” looks like

[(name1, value1), (name2, value2), (name3, value3), ...]
Answered By: David Pointer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.