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?
#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


Messages In This Thread
RE: Are list/dict comprehensions interpreted really sequentially? - by Gribouillis - May-31-2022, 08:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert this List Comprehensions to loop jacklee26 8 1,672 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,316 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Updating nested dict list keys tbaror 2 1,375 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,680 Jan-30-2021, 07:11 AM
Last Post: alloydog
  How to call multiple functions sequentially Mayo 2 9,724 Jan-06-2021, 07:37 PM
Last Post: Mayo
Question dict value, how to change type from int to list? swissjoker 3 2,910 Dec-09-2020, 09:50 AM
Last Post: perfringo
  How to remove dict from a list? Denial 7 3,179 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Trouble with converting list , dict to int values! faryad13 7 3,913 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,648 Aug-25-2020, 03:43 AM
Last Post: micseydel
  Sort a dict in dict cherry_cherry 4 96,446 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