Python Forum
Syntax when initializing empty lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax when initializing empty lists
#1
Hi all,

I have two questions with regard to initializing empty lists. This works:

list_a, list_b, list_c = ([] for i in range(3))
What exactly do the parentheses mean on the right? Without them I get a syntax error. With them, though, I don't recognize what kind of statement it makes. For example, it's not a list comprehension as that would be in brackets.

Second, on the left if I use equals signs instead of commas, then I get all three labels pointing to a generator object. Why?
Reply
#2
This is a generator. It will generate 3 empty lists.
([] for i in range(3))
You could ask Python what it is.
thing = ([] in range(3))
print(thing)
Output:
<generator object <genexpr> at 0x000002AA50964F90>
And we can test how many things it generates.
thing = ([] for i in range(3))
while True:
    print(next(thing))
Output:
[] [] [] Traceback (most recent call last): File "...", line 3, in <module> print(next(thing)) StopIteration
As predicted the generator "generates" 3 empty lists and then raises a StopIteration exception.

For the second question:

This does something called "unpacking":
a, b, c = (1, 2, 3)
There are 3 items in the tuple (1, 2, 3) and there are three variables on the left side of the the assignment. The three items are "unpacked" and assigned to the three variables. a = 1, b = 2, c = 3.

Unpacking works with an iterable. (1, 2, 3) is a tuple, and tuples are iterable. Generators are also iterable. That is why your code assigns different empty lists to a, b and c.


This assigns the same value to variables a, b and c:
a = b = c = (1, 2, 3)
It is like doing this:
c = (1, 2, 3)
b = c
a = b
In your code ([] for i in range(3)) is a generator. So doing "a = b = c = ([] for i in range(3)):" is like doing this:
c = ([] for i in range(3))
b = c
a = b
c cannot be assigned the three different lists from the generator, so Python assigns the generator to the variable a.
Reply
#3
Thanks for the detailed explanation, Dean!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 6,756 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 2,966 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  Initializing a PRINTDLG structure-how to hammer 4 1,434 Jun-08-2022, 07:07 PM
Last Post: jefsummers
  Initializing, reading and updating a large JSON file medatib531 0 1,722 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Lists + Empty Lists Pytho13 15 5,378 Mar-26-2021, 08:52 PM
Last Post: pythonprogrammer1101935
  Append 2d empty lists NMMST 2 2,789 Oct-19-2020, 09:25 PM
Last Post: NMMST
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Lists first item is a number however i cant compare it with an int getting syntax err Sutsro 4 2,359 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  Self re-initializing variable? python_user_n 6 3,119 May-03-2019, 02:52 PM
Last Post: Gribouillis
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,186 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020