Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack python tuple with [ ]'s [duplicate]

I know the canonical way to unpack a tuple is like this

a, b, c = (1, 2, 3)
# or
(a,b,c) = (1, 2, 3)

but noticed that you can unpack a tuple like this

[a, b, c] = (1, 2, 3)

Does the second method incur any extra cost due to some sort of cast or list construction? Is there a way to inspect how the python interpreter is dealing with this akin to looking at the assembly from a compiler?

like image 948
Philip Nelson Avatar asked Dec 01 '20 01:12

Philip Nelson


People also ask

How do you remove duplicates from tuple Python?

Method #1 : Using set() + tuple() This is the most straight forward way to remove duplicates. In this, we convert the tuple to a set, removing duplicates and then converting it back again using tuple().

Can tuple have duplicate values Python?

31.2 Python Collection Types Tuples A Tuple represents a collection of objects that are ordered and immutable (cannot be modified). Tuples allow duplicate members and are indexed. Lists Lists hold a collection of objects that are ordered and mutable (changeable), they are indexed and allow duplicate members.

How do you find duplicates in tuple Python?

Initial approach that can be applied is that we can iterate on each tuple and check it's count in list using count() , if greater than one, we can add to list. To remove multiple additions, we can convert the result to set using set() .

How to unpack a tuple in Python?

Generally, you can use the func(*tuple) syntax. You can even pass a part of the tuple, which seems like what you're trying to do here: This is called unpacking a tuple, and can be used for other iterables (such as lists) too.

What is the difference between packing and unpacking a tuple?

In other way it is called unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable. NOTE : In unpacking of tuple number of variables on left hand side should be equal to number of values in given tuple a.

What is unpacking in Python?

What’s unpacking? Unpacking is the process of getting out stuff — iterables such as lists, tuples, and dictionaries. Think of it as opening a box and getting out different items like cables, headphones, or a USB. Unpacking in Python is similar to unpack a box in real life. Let’s translate this same example into code for a better understanding:

What is an immutable tuple in Python?

Python tuples are immutable means that they can not be modified in whole program. Packing and Unpacking a Tuple : In Python there is a very powerful tuple assignment feature that assigns right hand side of values into left hand side. In other way it is called unpacking of a tuple of values into a variable.


2 Answers

No, those are all exactly equivalent. One way to look at this empirically is to use the dis dissasembler:

>>> import dis
>>> dis.dis("a, b, c = (1, 2, 3)")
  1           0 LOAD_CONST               0 ((1, 2, 3))
              2 UNPACK_SEQUENCE          3
              4 STORE_NAME               0 (a)
              6 STORE_NAME               1 (b)
              8 STORE_NAME               2 (c)
             10 LOAD_CONST               1 (None)
             12 RETURN_VALUE
>>> dis.dis("(a, b, c) = (1, 2, 3)")
  1           0 LOAD_CONST               0 ((1, 2, 3))
              2 UNPACK_SEQUENCE          3
              4 STORE_NAME               0 (a)
              6 STORE_NAME               1 (b)
              8 STORE_NAME               2 (c)
             10 LOAD_CONST               1 (None)
             12 RETURN_VALUE
>>> dis.dis("[a, b, c] = (1, 2, 3)")
  1           0 LOAD_CONST               0 ((1, 2, 3))
              2 UNPACK_SEQUENCE          3
              4 STORE_NAME               0 (a)
              6 STORE_NAME               1 (b)
              8 STORE_NAME               2 (c)
             10 LOAD_CONST               1 (None)
             12 RETURN_VALUE
>>>

From the formal language specification, this is detailed here. This is part of the "target list", A relevant quote:

Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows....

like image 80
juanpa.arrivillaga Avatar answered Oct 30 '22 20:10

juanpa.arrivillaga


Using godbolt and selecting Python as the language then entering the three lines of code, you can see they all have the same bytecode:

  1           0 LOAD_CONST               5 ((1, 2, 3))
              2 UNPACK_SEQUENCE          3
              4 STORE_NAME               0 (a)
              6 STORE_NAME               1 (b)
              8 STORE_NAME               2 (c)

  2          10 LOAD_CONST               6 ((1, 2, 3))
             12 UNPACK_SEQUENCE          3
             14 STORE_NAME               0 (a)
             16 STORE_NAME               1 (b)
             18 STORE_NAME               2 (c)

  3          20 LOAD_CONST               7 ((1, 2, 3))
             22 UNPACK_SEQUENCE          3
             24 STORE_NAME               0 (a)
             26 STORE_NAME               1 (b)
             28 STORE_NAME               2 (c)

So, they are the same, just different syntaxes.

like image 25
Aplet123 Avatar answered Oct 30 '22 20:10

Aplet123