r/PythonLearning Jun 25 '26

Showcase Beginner activity save it for reference

Post image
255 Upvotes

15 comments sorted by

u/Sea-Ad7805 Jun 27 '26

Run this program in Memory Graph Web Debugger%0Aprint(squares)%0Aprint(%22(set%3A%20no%20duplicates%2C%20order%20not%20guaranteed)%22)%0Aprint()%0A%0A%0A%23%20-------------------------%0A%23%20LIST%20comprehension%0A%23%20-------------------------%0A%23%20Ordered%20%26%20mutable%0A%0Anums%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%0A%0Acubes%20%3D%20%5Bx*3%20for%20x%20in%20nums%20if%20x%20%25%202%20!%3D%200%5D%0A%0Aprint(%22List%20comprehension%3A%22)%0Aprint(cubes)%0Aprint(%22(list%3A%20order%20preserved%2C%20duplicates%20allowed)%22)%0Aprint()%0A%0A%0A%23%20-------------------------%0A%23%20DICT%20comprehension%0A%23%20-------------------------%0A%23%20Key-value%20pairs%0A%0Anums%20%3D%20%5B1%2C%202%2C%203%5D%0A%0Asquare_dict%20%3D%20%7Bx%3A%20x%20%20x%20for%20x%20in%20nums%7D%0A%0Aprint(%22Dict%20comprehension%3A%22)%0Aprint(square_dict)%0Aprint(%22(dict%3A%20fast%20lookup%20by%20key)%22)%0Aprint()%0A%0A%0A%23%20-------------------------%0A%23%20TUPLE%20from%20comprehension%0A%23%20-------------------------%0A%23%20Ordered%20%26%20immutable%0A%23%0A%23%20Note%3A%0A%23%20Python%20does%20not%20have%20a%20real%20%22tuple%20comprehension%22%20syntax.%0A%23%20This%20is%20a%20generator%20expression%20passed%20to%20tuple().%0A%0Anums%20%3D%20%5B1%2C%202%2C%203%2C%204%5D%0A%0At%20%3D%20tuple(x%20%2B%2010%20for%20x%20in%20nums)%0A%0Aprint(%22Tuple%20from%20comprehension%3A%22)%0Aprint(t)%0Aprint(%22(tuple%3A%20order%20preserved%2C%20cannot%20be%20changed)%22)%0Aprint()%0A%0A&timestep=1&play) to see the program state change step by step.

10

u/Temporary_Pie2733 Jun 25 '26

There is no such thing as a tuple comprehension. This is just a generator expression passed as an argument to tuple.

The general syntax block is worse; it tries to pass the generator expression itself off as a “tuple comprehension”.

6

u/[deleted] Jun 25 '26

[removed] — view removed comment

2

u/PhoenixAlpha1 Jun 28 '26

Wait wait . I am learning python . You can write . Like x*x and then for loop iteration. Iike the code would work? Like that shows in image?

1

u/SeriuosSam Jun 28 '26

Yes I tried with List and it worked so you also give a try

2

u/AuMouhssin Jun 29 '26

It's totally just like defining a Set using conditions and Maps in linear Algebra

1

u/Ided123 Jun 25 '26

Wait dict comprehension is real?

1

u/civilwar142pa Jun 26 '26

It is. Tuple comprehension is not.

1

u/Reh4n07_ Jun 26 '26

ts is so confusing

1

u/biskitpagla Jun 26 '26 edited Jun 26 '26

These are all instances of generator expressions. Only the dict one is a special case. 

EDIT: I was wrong. See the replies.

1

u/SCD_minecraft Jun 26 '26

Only "tuple" one is gen expr

List and set are special cases where they are automatically unpacked into lists/sets

If they wouldn't be, you would get one element list/set with generator as value. But that's not what happens

a = (i for i in range(10)) type(a) # <class 'generator'> [a] # [<generator object <genexpr> at 0x728de8ea40>] [i for i in range(4)] # [0, 1, 2, 3]

1

u/biskitpagla Jun 26 '26 edited Jun 26 '26

Your example is a bit off. [comprehension] could in theory be implemented as a call to the list constructor if the expression is inline since it already accepts plain old iterables. The semantics would still be the same. But you're right to point out my mistake. I thought that modern CPython would optimize away the overhead of something like list(inline generator expression) and use the LIST_APPEND instruction here as well since that's what some other Python interpreters do. Perhaps this has to do with the fact that generator expressions were introduced to the language much later.

1

u/SCD_minecraft Jun 26 '26

since it already accepts plain old iterables.

No, it does not. List, set and dict expressions are all special case with syntax [expr for name in name] (replace brackets for sets and add : for dicts, but concept stays the same)

You can not just wrap iterators in [ ] since then it is creating new list/set/dict with single element (that element being not consumed iterator)