Python Forum
Are list/dict comprehensions interpreted really sequentially?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are list/dict comprehensions interpreted really sequentially?
#1
In any programming course/books I learned the intro section mentions the sequential
mode of execution of any program (implying at least the sequential way of navigating
through lines in an editor). But I suppose this sequential mode is also valid for
token-by-token processing.
Yet, in Python list and dictionary comprehensions, like
sum([i**2 for i in range(3)]) 
or
results = {n: n ** 2 for n in range(10)} 

the operational part of the for loop (i**2, n ** 2) comes before the definitive.
And it is not sequential. How does this happen in Python?
Reply
#2
It is not clear what you mean by «interpreted really sequentially».

In certain syntax constructs, Python executes code in a non left-to-right way. For example in
a = x + y if x * y > 2 else x - 1
the test x * y > 2 is executed first, then only one of x + y or x - 1 is executed.

Similarly in comprehension constructs, the for loops iterations are actually set up before the body is executed, but the body appears syntactically before the for loop.
Reply
#3
(May-31-2022, 08:24 PM)Gribouillis Wrote: In certain syntax constructs, Python executes code in a non left-to-right way.
How does this actually is implemented and what are these syntax constructs
(apart from already mentioned)?
To get to test x * y > 2, Python should first go through a = x + y.
Why it won't throw "a variable not defined" error?
Reply
#4
(May-31-2022, 08:37 PM)anata2047 Wrote: How does this actually is implemented
Python converts the code to an intermediate language named «bytecode». By using the dis module, one can examine this bytecode. Here is how python compiles the above conditional expression. It is apparent in the bytecode that the test in the middle is executed first. Of course, if x and y are not defined, an error will result.
>>> import dis
>>> dis.dis("a = x + y if x * y > 2 else x - 1")
  1           0 LOAD_NAME                0 (x)
              2 LOAD_NAME                1 (y)
              4 BINARY_MULTIPLY
              6 LOAD_CONST               0 (2)
              8 COMPARE_OP               4 (>)
             10 POP_JUMP_IF_FALSE       20
             12 LOAD_NAME                0 (x)
             14 LOAD_NAME                1 (y)
             16 BINARY_ADD
             18 JUMP_FORWARD             6 (to 26)
        >>   20 LOAD_NAME                0 (x)
             22 LOAD_CONST               1 (1)
             24 BINARY_SUBTRACT
        >>   26 STORE_NAME               2 (a)
             28 LOAD_CONST               2 (None)
             30 RETURN_VALUE
>>> 
For details about the syntax of Python and how it works, I strongly recommend reading The Python language reference.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,162 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,258 Jan-30-2021, 07:11 AM
Last Post: alloydog
  How to call multiple functions sequentially Mayo 2 9,161 Jan-06-2021, 07:37 PM
Last Post: Mayo
Question dict value, how to change type from int to list? swissjoker 3 2,694 Dec-09-2020, 09:50 AM
Last Post: perfringo
  How to remove dict from a list? Denial 7 2,853 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,468 Aug-25-2020, 03:43 AM
Last Post: micseydel
  Sort a dict in dict cherry_cherry 4 62,741 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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