How to unpack array in python

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

How to unpack array in python

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:

colors = ['red', 'blue', 'green']

Code language: Python (python)

To assign the first, second, and third elements of the list to variables, you may assign individual elements to variables like this:

red = colors[0] blue = colors[1] green = colors[2]

Code language: Python (python)

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:

red, blue, green = colors

Code language: Python (python)

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:

colors = ['red', 'blue', 'green'] red, blue = colors

Code language: Python (python)

Error:

ValueError: too many values to unpack (expected 2)

Code language: Python (python)

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:

colors = ['red', 'blue', 'green'] red, blue, *other = colors print(red) print(blue) print(other)

Code language: Python (python)

Output:

red blue ['green']

Code language: Python (python)

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:

colors = ['cyan', 'magenta', 'yellow', 'black'] cyan, magenta, *other = colors print(cyan) print(magenta) print(other)

Code language: Python (python)

Output:

cyan magenta ['yellow', 'black']

Code language: Python (python)

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 ?

How do I unpack an element in Python?

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.

How do you unpack a range in Python?

Unpacking in Python refers to assigning values in an iterable object to a tuple or list of variables for later use. We can perform unpacking by using an asterisk ( * ) before the object we would like to unpack.

Can you unpack Numpy array?

To unpack elements of a uint8 array into a binary-valued output array, use the numpy. unpackbits() method in Python Numpy. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done.

Can we unpack lists in Python?

We can unpack two Python lists into a single variable. It will return a tuple containing all the elements from both lists.