Python Forum
Syntax when initializing empty lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax when initializing empty lists
#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


Messages In This Thread
Syntax when initializing empty lists - by Mark17 - Jun-01-2022, 05:19 PM
RE: Syntax when initializing empty lists - by deanhystad - Jun-01-2022, 09:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 7,110 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,241 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  Initializing a PRINTDLG structure-how to hammer 4 1,532 Jun-08-2022, 07:07 PM
Last Post: jefsummers
  Initializing, reading and updating a large JSON file medatib531 0 1,816 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Lists + Empty Lists Pytho13 15 5,659 Mar-26-2021, 08:52 PM
Last Post: pythonprogrammer1101935
  Append 2d empty lists NMMST 2 2,995 Oct-19-2020, 09:25 PM
Last Post: NMMST
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,445 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,457 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  Self re-initializing variable? python_user_n 6 3,246 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,320 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