itemgetter() in Python Last Updated : 21 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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)) Output30 Explanation:itemgetter(2) creates a callable object that retrieves the item at index 2.The list a contains: Index 0 → 10Index 1 → 20Index 2 → 30val(lst) returns the value at index 2 → 30.Syntaxfrom operator import itemgetteritemgetter(index1, index2, ...)Parametersindex1, index2, ...: The positions of elements to extract.Return ValueReturns the value(s) at the specified index or key.When multiple indices are used, it returns a tuple containing the values.Examples of itemgetter() method1. Extracting Multiple Elements Python from operator import itemgetter a = [10, 20, 30, 40] val = itemgetter(1, 3) print(val(a)) Output(20, 40) 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.3. Extracting Values from Dictionaries Python from operator import itemgetter d = {"a": 10, "b": 20, "c": 30} get_value = itemgetter("b") print(get_value(d)) Output20 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. Comment More infoAdvertise with us Next Article itemgetter() in Python P pragatikheuvg Follow Improve Article Tags : Python Python Programs Practice Tags : python 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 How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it 2 min read Iterate Python Dictionary Using Enumerate() Function Python dictionaries are versatile data structures used to store key-value pairs. When it comes to iterating through the elements of a dictionary, developers often turn to the enumerate() function for its simplicity and efficiency. In this article, we will explore how to iterate through Python dictio 3 min read Append Elements to Empty List in Python In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list. 2 min read Python Unpack List Unpacking lists in Python is a feature that allows us to extract values from a list into variables or other data structures. This technique is useful for various situations, including assignments, function arguments and iteration. In this article, weâll explore what is list unpacking and how to use 3 min read tuple() Constructor in Python In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list, 2 min read Like