1

There is a formatting question, that I would like some additional insight/understanding on. I have the following code:

In[1]:
mylist = [[99]]
for [x] in mylist:
 print(x)
Out[1]:
99

My main question is regarding the []s around the x in the second line. So I have never used []s before when describing the 'x' variable of a loop. Since my output is 99 and not [99], it looks like the []s are going to ask the loop to extract the number from its additional set of brackets.

Question Updated Based on Responses:

If I change the code to remove the brackets:

In[1]:
mylist = [[99]]
for x in mylist:
 print(x)
Out[1]:
[99]

I get [99] instead of 99. However, if I do the following:

In[1]:
mylist = [[99,100]]
for x,y in mylist:
 print(x)
 print(y)
Out[1]:
99
100

This example above doesn't require an additional set of []s around x,y and produces a bracket-less answer in the output, unlike the previous two examples, which requires a [] to produce a bracket-less answer.

I realize this is an odd and fairly silly question since I would never construct a single element list like this. I simply saw this being casually used in an answer elsewhere online (with no explanation on it unfortunately). Being a newcomer, I'm just curious to broaden my understanding of the language.

Thanks in advance.

asked Oct 11, 2018 at 23:33
1
  • You would have to change your list to: [[99],[100]] You're attempting to unpack items in the list as single-element lists, not multiple element lists. Commented Oct 11, 2018 at 23:37

3 Answers 3

2

When you do this:

>>> myList = [[99]]
>>> for x in myList:
 print x

Python interprets that as "print each element in this iterable".

When you do this:

>>> myList = [[99,100], [99,101], [99, 102]]
>>> for x in myList:
 print x

Python still interprets that as "print each element in this iterable" so that you get this:

[99, 100]
[99, 101]
[99, 102]

But, if you do this:

>>> myList = [[99,100], [99,101], [99, 102]]
>>> for x, y in myList:
 print x, y

Python will "unpack" the values for you from each element in the iterable and assign them to x and y. If you want to do that above for only the case of myList = [[99]], Python requires the for [x] in myList syntax so that it unpacks the single value from the list.

The ability to "unpack" an iterable is very powerful in Python. You can assign variables on the fly by unpacking an iterable. In your case, you can imagine having to assign lat lon as variables maybe or something else. You can also unpack values into the args of a function.

In Python 3 you can also do something like this:

x = [1,2,3,4]
first, *rest = x
print (first) # 1
print (rest) # [2,3,4]
answered Oct 11, 2018 at 23:59
Sign up to request clarification or add additional context in comments.

1 Comment

Great information. Just what I was looking for. Thank you very much!
1

For the second example, with lists of length 2, to unpack both values you can do

In[1]:
myList = [[99,100]]
for x, y in myList: 
 print(x)
 print(y)
Out[1]:
99
100
answered Oct 11, 2018 at 23:42

2 Comments

What I find interesting with that is you didn't have to surround x,y with []s to get the output to be the number by themselves. Going back to my original example, if I do not surround x with brackets, ie 'for x in myList'....the print out would be [99] instead of 99, so just the special use case of those []s is whats peaked my interest. Your example helps though
I see. In that example, if you use x without brackets, each item in myList is a list as well, so if you print x, it is going to be a list. When the you use the brackets, you are basically saying that you know that each item in myList is a list of one element, and x is what is inside each list, so the value in unpacked. You could do the same thing for my example above e.g. for [x,y] in myList, but python allows you to not provide the brackets for multi element lists/tuples because if you specify more than one, it knows there is unpacking that needs to be done
1

As per Python documentation, the square brackets are essentially ignored in assignments and loop constructs.

So as a complement to user2774697's answer, for x, y in L is equivalent to for [x, y] in L which is equivalent to for ([x, y]) in L and is also equivalent to for (x, y) in L.

The thing that the square brackets do different than a bare round parenthesis is that it enforces an unpacking behavior, it requires the elements in L to be iterable.

answered Oct 12, 2018 at 0:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.