How to get specific elements from list in python
Last Updated :
16 Nov, 2024
In this article, we will learn how to get specific item(s) from given list. There are multiple methods to achieve this. Most simple method is accessing list item by Index.
Python
a = [1, 'geeks', 3, 'for', 5]
# accessing specific list item with their indices
item1 = a[1]
item3 = a[2]
item5 = a[4]
# negative indexing to get last element, same as a[4]
item_5 = a[-1]
print(item1, item3, item5, item_5)
To get multiple items at once through index, you can create a list of indices and iterate to access those items.
Python
a = [1, 'geeks', 3, 'for', 5]
# list of indices
index = [1, 2, 4]
for i in index:
print(a[i])
# comprehensive way of writing above for loop
# print([a[i] for i in index])
Using List Slicing [ to access range of elements]
By using slicing, you can access range of elements by specifying start and end index.
Python
a = [1, 'geeks', 3, 'for', 5]
# Accessing elements from index 1 to 3 (excluding 4)
a1 = a[1:4] # ['geeks', 3, 'for']
# Accessing elements from start to index 3 (excluding 4)
a2 = a[:4] # [1, 'geeks', 3, 'for']
# Accessing elements from index 2 to the end
a3 = a[2:] # [3, 'for', 5]
# Accessing every second element
a4 = a[::2] # [1, 3, 5]
print(a1, a2, a3, a4)
Output['geeks', 3, 'for'] [1, 'geeks', 3, 'for'] [3, 'for', 5] [1, 3, 5]
Using Slicing is unsuitable if the indices you want are not contiguous or don't follow regular pattern.
Using itemgetter()
Function [Pythonic and Efficient Approach]
operator.itemgetter function gives better performance as compared to For Loop or comprehension. It can take multiple indices at once and return a tuple.
Python
from operator import itemgetter
a = [1, 'geeks', 3, 'for', 5]
a1 = itemgetter(1, 3)(a)
print(a1)
Get specific elements from a List (based on condition)
To get the elements based on some condition, you can use List comprehension and filter() method. Let's understand them with help of examples.
Using List Comprehension -
Python
a = [10, 20, 30, 40, 50]
# Extract elements greater than 20
a1 = [i for i in a if i > 20]
print(a1)
Using filter() method -
Let's do the same operation as above, using filter() method with lambda function.
Python
a = [10, 20, 30, 40, 50]
# Filter with a lambda function
a1 = list(filter(lambda x: x > 20, a)) # [30, 40, 50]
print(a1)
Similar Reads
Get first element from a List of tuples - Python The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
2 min read
Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
How to Split Lists in Python? Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
3 min read
Get the Last Element of List in Python In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list:Input: list = [1, 3, 34, 12, 6]Output: 6Explanation: Last element of the list l in the above example is 6.Let's explore various methods of doing it in Python:1. Using Neg
2 min read