0

Does the usage of parentheses have any effect whatsoever in the variable declaration syntax of a Python for loop?

Example 1: basic for loop declaring i with no parenthesis

>>> for i in range(0,10): print(i)
...
0
1
2
3
4
5
6
7
8
9

Basic for loop declaring i with arbitrary parentheses

for (((((i))))) in range(0,10): print(i)
...
0
1
2
3
4
5
6
7
8
9

For loop which unpacks two values, declared with no parentheses

>>> for x,y in zip(range(0,10), range(0,10)):  print(x,y)
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

Same thing, but with parenthesis around x, y, and both.

>>> for ((x),(y)) in zip(range(0,10), range(0,10)):  print(x,y)
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

So it seems the parentheses have no effect - they are not interpreted as creating, for example, a tuple. Am I correct, or is there any reason to use parentheses in a for loop variable declaration?

You can even, apparently, say this:

>>> for [x,y] in zip(range(0,10), range(0,10)):  print(x,y)
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

...and the list notation (brackets) appear to have no effect either.

1
  • No, there's not. Parentheses doesn't do anything in your code. If you want to construct a tuple, do it as (value,) Commented Jul 9, 2014 at 15:34

3 Answers 3

5

Just parenthesis around an expression only act to group the expression (to override operator precedence, to be able to span multiple lines, etc.).

If you wanted to create tuples, use a comma:

>>> 1
1
>>> 1,
(1,)
Sign up to request clarification or add additional context in comments.

Comments

2

is there any reason to use parentheses in a for loop variable declaration?

Yes, you need them to unpack more complex iterables. Specifically, nested iterables such as this one:

enumerate(zip(range(10), range(10, 20)))

Using parenthesis, everything works fine:

>>> for x, (y, z) in enumerate(zip(range(10), range(10, 20))):
...    print("x=", x, "y=", y, "z=", z)
...
x= 0 y= 0 z= 10
x= 1 y= 1 z= 11
x= 2 y= 2 z= 12
x= 3 y= 3 z= 13
x= 4 y= 4 z= 14
x= 5 y= 5 z= 15
x= 6 y= 6 z= 16
x= 7 y= 7 z= 17
x= 8 y= 8 z= 18
x= 9 y= 9 z= 19
>>>

because x, (y, z) matches the structure of the iterables returned by:

enumerate(zip(range(10), range(10, 20)))

Without the parenthesis however, you will raise a ValueError:

>>> for x, y, z in enumerate(zip(range(10), range(10, 20))):
...    print("x=", x, "y=", y, "z=", z)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>>

because Python sees x, y, z and therefore expects:

enumerate(zip(range(10), range(10, 20)))

to return three-item iterables when it only returns two-item ones (tuples with a number and another two-item tuple):

>>> for item in enumerate(zip(range(10), range(10, 20))):
...     print(item)
...
(0, (0, 10))
(1, (1, 11))
(2, (2, 12))
(3, (3, 13))
(4, (4, 14))
(5, (5, 15))
(6, (6, 16))
(7, (7, 17))
(8, (8, 18))
(9, (9, 19))
>>>

Comments

2

With the exception of the empty tuple (), it is not the parentheses which define a tuple, it's the commas. Parentheses simply keep the tuple items separate from surrounding code.

# These are all identical: x is a tuple with 2 elements.
x = (1, 2)
(x) = (1, 2)
x = 1, 2
(x) = 1, 2

# These are all identical: x is assigned the first element of the tuple (1, 2)
x, = (1, 2)
(x,) = (1, 2)
x, = 1, 2
(x,) = 1, 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.