The itemgetter() function from the operator module in Python is used to extract specific items from a list, tuple, or dictionary. It allows easy retrieval of elements without writing lambda functions.
Example:
Python
from operator import itemgetter
a = [10, 20, 30, 40]
val = itemgetter(2)
print(val(a))
Explanation:
- itemgetter(2) creates a callable object that retrieves the item at index 2.
- The list a contains:
- Index 0 → 10
- Index 1 → 20
- Index 2 → 30
- val(lst) returns the value at index 2 → 30.
Syntax
from operator import itemgetteritemgetter(index1, index2, ...)
Parameters
- index1, index2, ...: The positions of elements to extract.
Return Value
- Returns the value(s) at the specified index or key.
- When multiple indices are used, it returns a tuple containing the values.
Examples of itemgetter() method
Python
from operator import itemgetter
a = [10, 20, 30, 40]
val = itemgetter(1, 3)
print(val(a))
Explanation: This code uses itemgetter() from the operator module to create a callable that retrieves elements at the specified indices (1 and 3) from the list lst. The get_items(lst) call fetches the elements at indices 1 and 3 (which are 20 and 40), and the result (20, 40) is printed.
2. Using itemgetter() for Sorting
Python
from operator import itemgetter
a = [("Rahul", 85), ("Raj", 90), ("Jay", 80)]
b = sorted(s, key=itemgetter(1))
print(b)
Output[('Jay', 80), ('Rahul', 85), ('Raj', 90)]
Explanation: This code sorts a list of tuples, students, based on the second element (the score) using itemgetter(1) from the operator module. The sorted() function sorts the tuples in ascending order by the scores, and the result is stored in sorted_students and printed.
Python
from operator import itemgetter
d = {"a": 10, "b": 20, "c": 30}
get_value = itemgetter("b")
print(get_value(d))
Explanation: This code uses itemgetter() from the operator module to create a callable that retrieves the value associated with the key "b" from the dictionary d. The get_value(d) call fetches the value corresponding to the key "b" (which is 20), and the result 20 is printed.
Similar Reads
Python - Access List Item Whether we are working with numbers, strings or other data types, lists provide a versatile way to organize and manipulate data. But how to access specific items in a list? This article will guide you through various methods of accessing list items in Python.Accessing List Items by IndexIn Python, l
3 min read
Python Class Members Python, similarly to other object-oriented allows the user to write short and beautiful code by enabling the developer to define classes to create objects. The developer can define the prototype of an object class based on two types of members: Instance membersClass members In the real world, these
6 min read
Python Access Set Items Python sets are unordered collections of unique elements. Unlike lists or dictionaries, sets do not have indices so accessing elements in the traditional way using an index doesn't apply. However, there are still ways to work with sets and access their items effectively. This article will guide you
2 min read
Python Access Tuple Item In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr
2 min read
Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read