How does python "know" what to do with the "in" keyword?
Question:
I’m a bit bewildered by the “in” keyword in python.
If I take a sample list of tuples:
data = [
(5, 1, 9.8385465),
(10, 1, 8.2087544),
(15, 1, 7.8788187),
(20, 1, 7.5751283)
]
I can do two different “for – in” loops and get different results:
for G,W,V in data:
print G,W,V
This prints each set of values on a line, e.g. 5, 1, 9.8385465
for i in data:
print i
This prints the whole tuple, e.g. (5, 1, 9.8385465)
How does python “know” that by providing one variable I want to assign the tuple to a variable, and that by providing three variables I want to assign each value from the tuple to one of those variables?
Answers:
Iterators notionally return a single item (the for a in b
syntax).
The compiler knows that if you specify more than one thing for a
that the one returned value must itself be iterable and contain the same number of items as your list of variables. It would throw a runtime error if you had for a, b in c
and the elements of c
did not contain exactly two items.
So in short it “knew” because that is what you told it. It will explode at runtime if you lied …
It’s called tuple unpacking, and has nothing to do with the in
keyword.
The for
loop returns the single thing (a tuple
in this case), and then that tuple
gets assigned — to a single item in the second case, or multiple items in the first case.
If you try specifying the incorrect number of variables:
for G,W in data:
print G,W
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
According to the for
compound statement documentation:
Each item in turn is assigned to the target list using the standard
rules for assignments…
Those “standard rules” are in the assignment statement documentation, specifically:
Assignment of an object to a target list is recursively defined as
follows.
-
If the target list is a single target: The object is assigned to that target.
-
If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets
in the target list, and the items are assigned, from left to right, to
the corresponding targets.
So this different behaviour, depending on whether you assign to a single target or a list of targets, is baked right into Python’s fundamentals, and applies wherever assignment is used.
This isn’t really a feature of the in
keyword, but of the Python language. The same works with assignment.
x = (1, 2, 3)
print(x)
>>> (1, 2, 3)
a, b, c = (1, 2, 3)
print(a)
>>> 1
print(b)
>>> 2
print(c)
>>> 3
So to answer your question, it’s more that Python knows what to do with assignments when you either:
- assign a tuple to a variable, or
- assign a tuple to a number of variables equal to the number of items in the tuple
I’m a bit bewildered by the “in” keyword in python.
If I take a sample list of tuples:
data = [
(5, 1, 9.8385465),
(10, 1, 8.2087544),
(15, 1, 7.8788187),
(20, 1, 7.5751283)
]
I can do two different “for – in” loops and get different results:
for G,W,V in data:
print G,W,V
This prints each set of values on a line, e.g. 5, 1, 9.8385465
for i in data:
print i
This prints the whole tuple, e.g. (5, 1, 9.8385465)
How does python “know” that by providing one variable I want to assign the tuple to a variable, and that by providing three variables I want to assign each value from the tuple to one of those variables?
Iterators notionally return a single item (the for a in b
syntax).
The compiler knows that if you specify more than one thing for a
that the one returned value must itself be iterable and contain the same number of items as your list of variables. It would throw a runtime error if you had for a, b in c
and the elements of c
did not contain exactly two items.
So in short it “knew” because that is what you told it. It will explode at runtime if you lied …
It’s called tuple unpacking, and has nothing to do with the in
keyword.
The for
loop returns the single thing (a tuple
in this case), and then that tuple
gets assigned — to a single item in the second case, or multiple items in the first case.
If you try specifying the incorrect number of variables:
for G,W in data:
print G,W
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
According to the for
compound statement documentation:
Each item in turn is assigned to the target list using the standard
rules for assignments…
Those “standard rules” are in the assignment statement documentation, specifically:
Assignment of an object to a target list is recursively defined as
follows.
If the target list is a single target: The object is assigned to that target.
If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets
in the target list, and the items are assigned, from left to right, to
the corresponding targets.
So this different behaviour, depending on whether you assign to a single target or a list of targets, is baked right into Python’s fundamentals, and applies wherever assignment is used.
This isn’t really a feature of the in
keyword, but of the Python language. The same works with assignment.
x = (1, 2, 3)
print(x)
>>> (1, 2, 3)
a, b, c = (1, 2, 3)
print(a)
>>> 1
print(b)
>>> 2
print(c)
>>> 3
So to answer your question, it’s more that Python knows what to do with assignments when you either:
- assign a tuple to a variable, or
- assign a tuple to a number of variables equal to the number of items in the tuple