The unzip operation is:
In [1]: data = [[1,2],[40,2],[9,80]]
In [2]: zip[*data]
Out[2]: [[1, 40, 9], [2, 2, 80]]
Edit: You can decompose the resulting list on assignment:
In [3]: first_elements, second_elements = zip[*data]
And if you really need lists as results:
In [4]: first_elements, second_elements = map[list, zip[*data]]
To better understand why this works:
zip[*data]
is equivalent to
zip[[1,2], [40,2], [9,80]]
The two tuples in the result list are built from the first elements of zip[]'s arguments and from the second elements of zip[]'s arguments.
Let’s learn the different ways to extract elements from a Python list When more than one item is required to be stored in a single variable in Python, we need to use lists. It is one of python’s built-in data functions. It is created by using [ ] brackets while initializing a variable. In this article, we are going to see the different ways
through which lists can be created and also learn the different ways through which elements from a list in python can be extracted. Here in this first example, we created a list named ‘firstgrid’ with 6 elements in it. The print statement prints the ‘1’ element in the index.1. Extract Elements From A Python List Using Index
firstgrid=["A","B","C","D","E","F"]
print[firstgrid[1]]
2. Print Items From a List Using Enumerate
Here, we created a variable named ‘vara’ and we filled the elements into the list. Then we used ‘varx’ variable to specify the enumerate function to search for ‘1,2,5’ index positions.
vara=["10","11","12","13","14","15"] print[[varx[1] for varx in enumerate[vara] if varx[0] in [1,2,5]]]
Output: ['11', '12', '15']
3. Using Loops to Extract List Elements
You can also Extract Elements From A Python List using loops. Let’s see 3 methods to pull individual elements from a list using loops.
Method 1:
Directly using a loop to search for specified indexes.
vara=["10","11","12","13","14","15"] print[[vara[i] for i in [1,2,5]]]
Output: ['11', '12', '15']
Method 2:
Storing list and index positions into two different variables and then running the loop to search for those index positions.
elements = [10, 11, 12, 13, 14, 15] indices = [1,1,2,1,5] result_list = [elements[i] for i in indices] print[result_list]
Output: [11, 11, 12, 11, 15]
Method 3:
In this example, we used a different way to create our list. The range function creates a list containing numbers serially with 6 elements in it from 10 to 15.
numbers = range[10, 16] indices = [1, 1, 2, 1, 5] result = [numbers[i] for i in indices] print[result]
Output: [12, 11, 11, 14, 15]
4. Using Numpy To View Items From a List
We can also use the popular NumPy library to help us Extract Elements From A Python List. Let’s see how that can be done here using two different methods.
Method 1:
Here, we used the numpy import function to print the index specified in variable ‘sx’ from the elements present in list ‘ax’ using np.array library function.
ax = [10, 11, 12, 13, 14, 15]; sx = [1, 2, 5] ; import numpy as np print[list[np.array[ax][sx]]]
Method 2:
This example uses variable storing index positions and another variable storing numbers in an array. The print statement prints the index positions stored in variable ‘sx’ with respect to a variable containing the list – ‘ay’.
sx = [1, 2, 5]; ay = np.array[[10, 11, 12, 13, 14, 15]] print[ay[sx]]
5. Extract Elements Using The index function
The index function specifies the program to search for given indexes mentioned in brackets and then runs a loop to check for the indexes present. The statement, ‘0