## Understanding *x ,= lst

unpacking iterable python

can use starred expression only as assignment target

python 2 starred expression

python 3 tuple unpacking

pep 3132

python unpack ellipsis

python starred expression

I'm going through some old code trying to understand what it does, and I came across this odd statement:

*x ,= p

`p`

is a list in this context. I've been trying to figure out what this statement does. As far as I can tell, it just sets `x`

to the value of `p`

. For example:

p = [1,2] *x ,= p print(x)

Just gives

[1, 2]

So is this any different than `x = p`

? Any idea what this syntax is doing?

`*x ,= p`

is basically an obfuscated version of `x = list(p)`

using extended iterable unpacking. The comma after `x`

is required to make the assignment target a tuple (it could also be a list though).

`*x, = p`

**is** different from `x = p`

because the former creates a *copy* of `p`

(i.e. a new list) while the latter creates a *reference* to the original list. To illustrate:

>>> p = [1, 2] >>> *x, = p >>> x == p True >>> x is p False >>> x = p >>> x == p True >>> x is p True

**5. Data Structures — Python 2.7.18 documentation,** Return the index in the list of the first item whose value is x. arguments can be used for sort customization, see sorted() for their explanation). First, it takes the X(0) from the sequence of input and then it outputs h(0) which together with X(1) is the input for the next step. So, the h(0) and X(1) is the input for the next step. Similarly, h(1) from the next is the input with X(2) for the next step and so on. This way, it keeps remembering the context while training.

It's a feature that was introduced in Python 3.0 (PEP 3132). In Python 2, you could do something like this:

>>> p = [1, 2, 3] >>> q, r, s = p >>> q 1 >>> r 2 >>> s 3

Python 3 extended this so that one variable could hold multiple values:

>>> p = [1, 2, 3] >>> q, *r = p >>> q 1 >>> r [2, 3]

This, therefore, is what is being used here. Instead of two variables to hold three values, however, it is just one variable that takes each value in the list. This is different from `x = p`

because `x = p`

just means that `x`

is another name for `p`

. In this case, however, it is a new list that just happens to have the same values in it. (You may be interested in "Least Astonishment" and the Mutable Default Argument)

Two other common ways of producing this effect are:

>>> x = list(p)

and

>>> x = p[:]

Since Python 3.3, the list object actually has a method intended for copying:

x = p.copy()

The slice is actually a very similar concept. As nneonneo pointed out, however, that works only with objects such as lists and tuples that support slices. The method you mention, however, works with any iterable: dictionaries, sets, generators, etc.

**List Comprehensions in Python,** The list comprehension always returns a result list. x = [i for i in range(10)] print x # This will give the output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. In the above diagram, a chunk of neural network, \(A\), looks at some input \(x_t\) and outputs a value \(h_t\). A loop allows information to be passed from one step of the network to the next. These loops make recurrent neural networks seem kind of mysterious.

You should always throw these to `dis`

and see what it throws back at you; you'll see how `*x, = p`

is actually different from `x = p`

:

dis('*x, = p') 1 0 LOAD_NAME 0 (p) 2 UNPACK_EX 0 4 STORE_NAME 1 (x)

While, the simple assignment statement:

dis('x = p') 1 0 LOAD_NAME 0 (p) 2 STORE_NAME 1 (x)

(Stripping off unrelated `None`

returns)

As you can see `UNPACK_EX`

is the different op-code between these; it's documented as:

Implements assignment with a starred target: Unpacks an iterable in TOS (top of stack) into individual values, where the total number of values can be smaller than the number of items in the iterable: one of the new values will be a list of all leftover items.

Which is why, as Eugene noted, you get a new object that's referred to by the name `x`

and not a reference to an already existing object (as is the case with `x = p`

).

`*x,`

does seem very odd (the extra comma there and all) but it is required here. The left hand side must either be a tuple or a list and, due to the quirkiness of creating a single element tuple in Python, you need to use a trailing `,`

:

i = 1, # one element tuple

If you like confusing people, you can always use the `list`

version of this:

[*x] = p

which does exactly the same thing but doesn't have that extra comma hanging around there.

**(Tutorial) Python List Comprehension,** Learn how to effectively use list comprehension in Python to create lists, to replace S = [x**2 for x in range(10)] V = [2**i for i in range(13)] S LST Galaxia is an engaging and interactive educational game designed to prevent bullying, cyberbullying, substance use, and violence among middle school youth. Since middle school is a particularly high risk period for bullying and cyberbullying, LST Galaxia is a perfect complement to the Botvin LifeSkills Training (LST) Middle School program

**You can clearly understand it from below example**

L = [1, 2, 3, 4] while L: temp, *L = L print(temp, L)

what it does is, the front variable will get the first item every time and the remaining list will be given to L.

The output will look shown below.

1 [2, 3, 4] 2 [3, 4] 3 [4] 4 []

Also look at below example

x, *y, z = "python" print(x,y,z)

In this both x,z will get each one letter from the string meaning first letter is assigned to x and the last letter will be assigned to z and the remaining string will be assigned to variable y.

p ['y', 't', 'h', 'o'] n

One more example,

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

**Boundary case: If there is nothing remaining for star variable then it will get an empty list.**

Example:

a,b=[1] print(a,b) 1 []

**Understanding the List Data Structure in Python 3,** If we want to include either end of the list, we can omit one of the numbers in the list[x:y] syntax. For example, if we want to print the first 3 items� lst() constructs a list, similar to base::list(), but with some of the same features as tibble(). lst() builds components sequentially. When defining a component, you can refer to components created earlier in the call. lst() also generates missing names automatically.

**Lists and Tuples in Python – Real Python,** Watch it together with the written tutorial to deepen your understanding: Lists and In short, a list is a collection of arbitrary objects, somewhat akin to an array in x[1][1] is yet another sublist, so adding one more index accesses its elements:. The British Council’s four Understanding IELTS courses will give you a complete guide to everything you need to know as you prepare for the IELTS test. This course focuses on the reading test. You’ll explore the format of the test and the kinds of text you need to read, and develop learning strategies to help you tackle the different task

**Python Tutorial: List Comprehension,** In mathematics the square numbers of the natural numbers are, for example, created by { x2 | x ∈ ℕ } or the set of complex integers { (x,y) | x ∈ ℤ ∧ y ∈ ℤ }. List� Exponential functions follow all the rules of functions. However, because they also make up their own unique family, they have their own subset of rules. The following list outlines some basic rules that apply to exponential functions: The parent exponential function f(x) = bx always has a horizontal asymptote at y = 0, except when […]

**Python List (With Examples),** A list comprehension consists of an expression followed by for statement 11, 13, 15, 17, 19] >>> [x+y for x in ['Python ','C '] for y in ['Language','Programming']]� Ibram X. Kendi, a professor and director of the Antiracist Research & Policy Center at American University, is the National Book Award-winning author of “Stamped From the Beginning: The

##### Comments

- It's different because instead of assigning an alias, it copies the list.
- See python.org/dev/peps/pep-3132
- Omitting the comma gives an error message to which this might be an interesting reference: "SyntaxError: starred assignment target must be in a list or tuple".
- Also worth noting that the comma actually belongs to the
`*x`

, as the starred assignment must be in a list or tuple. So the more explicit way to write that statement is`(*x,) = p`

- Important to note, this works when
`p`

is*any*iterable. - Might be worth adding something like
`x[0] = 3; p #=> [1, 2]`

to the first half and`x[0] = 3; p #=> [3, 2]`

to the second, to illustrate why`is`

and`==`

are different - Note, though, that your second bit of code,
`x = p[:]`

requires that`p`

be slice-able. This excludes things like generators. - Wait, 3.0!? I thought it was way more recent, 3.5 or 3.6 or something.
- @user2357112: In What's New In Python 3.0, you will find this bullet point:
`PEP 3132 was accepted`

. In addition, the PEP itself says the Python version is 3.0. I'm pretty sure that must be the case, then. - @zondo In Python3, the slicing method of copying has a new more readable counterpart that you might include instead since this question only applies to Python3. (Though only Python 3.3 and up have it.)
- @River: Ah, yes. I had forgotten about that one. I kept the slice because it is useful for more than just lists and is still a commonly-seen tactic, but I added the
`.copy`

method.