I have a variable data that is of [1000L, 3L]
shape and I do the following to get the coordinates:
x = data[:,0]
y = data[:,1]
z = data[:,2]
Is there a way to unpack them? I tried but it doesn't work:
[x,y,z] = data1[:,0:3]
MSeifert
137k32 gold badges318 silver badges333 bronze badges
asked Sep 9, 2017 at 17:44
You could simply transpose it before unpacking:
x, y, z = data.T
Unpacking "unpacks" the first dimensions of an array and by transposing the your array the size-3 dimension will be the first dimension. That's why it didn't work with [x, y, z] = data1[:, 0:3]
because that tried to
unpack 1000 values into 3 variables.
answered Sep 9, 2017 at 17:45
MSeifertMSeifert
137k32 gold badges318 silver badges333 bronze badges
0
You could unpack using zip
:
x, y, z = zip[*data[:, :3]]
answered Sep 9, 2017 at 17:51
AlexanderAlexander
99.3k28 gold badges187 silver badges185 bronze badges
Summary: in this tutorial, you’ll learn how to unpack a list in Python to make your code more concise.
Introduction to the list unpacking
The following example defines a list of strings:
Code language: Python [python]
colors = ['red', 'blue', 'green']
To assign the first, second, and third elements of the list to variables, you may assign individual elements to variables like this:
Code language: Python [python]
red = colors[0] blue = colors[1] green = colors[2]
However, Python provides a better way to do this. It’s called sequence unpacking.
Basically, you can assign elements of a list [and also a tuple] to multiple variables. For example:
Code language: Python [python]
red, blue, green = colors
This statement assigns the first, second, and third elements of the colors
list to the red
, blue
, and green
variables.
In this example, the number of variables on the left side is the same as the number of elements in the list on the right side.
If you use a fewer number of variables on the left side, you’ll get an error. For example:
Code language: Python [python]
colors = ['red', 'blue', 'green'] red, blue = colors
Error:
Code language: Python [python]
ValueError: too many values to unpack [expected 2]
In this case, Python could not unpack three elements to two variables.
Unpacking and packing
If you want to unpack the first few elements of a list and don’t care about the other elements, you can:
- First, unpack the needed elements to variables.
- Second, pack the leftover elements into a new list and assign it to another variable.
By putting the asterisk [*
] in front of a variable name, you’ll pack the
leftover elements into a list and assign them to a variable. For example:
Code language: Python [python]
colors = ['red', 'blue', 'green'] red, blue, *other = colors print[red] print[blue] print[other]
Output:
Code language: Python [python]
red blue ['green']
This example assigns the first and second elements of the colors
list to the red
and green
variables. And it assigns the last element of the list to the other
variable.
Here’s another example:
Code language: Python [python]
colors = ['cyan', 'magenta', 'yellow', 'black'] cyan, magenta, *other = colors print[cyan] print[magenta] print[other]
Output:
Code language: Python [python]
cyan magenta ['yellow', 'black']
This example assigns the first and second elements to variables. It packs the last two elements in a new list and assigns the new list to the
other
variable.
Summary
- Unpacking assigns elements of the list to multiple variables.
- Use the asterisk [*] in front of a variable like this *variable_name to pack the leftover elements of a list into another list.
Did you find this tutorial helpful ?