5 Easy Ways To Extract Elements From A Python List

How To Extract Elements Png

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.

1. Extract Elements From A Python List Using Index

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.

firstgrid=["A","B","C","D","E","F"]

print(firstgrid[1])
Output: 'B'

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]))
Output: [11, 12, 15]

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])
Output: [11 12 15]

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 <= index < len(vara)’ tell the compiler to search for only the index positions between 0 and the last index which is specified by ‘len(variable)’. For that reason, we can see only 3 elements in the output though the program tells the compiler to search for 4 index positions. The loop discards any index position that is out of the given range.

vara=["10","11","12","13","14","15"]
print([vara[index] for index in (1,2,5,20) if 0 <= index < len(vara)])
Output: ['13', '12', '14']

Conclusion

This article explains in great detail the different methods available to search and extract elements from a python list. We learned in this article, how lists are made, the different types of python functions through which elements are extracted from the list. It is hoped that this article might have helped you.