Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get first element of each sublist in Python
A list in Python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list.
Using for Loop
It is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below ?
Example
nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:\n", nested_list)
print("First Items from sublists:")
for item in nested_list:
print(item[0])
The output of the above code is ?
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: Mon Tue 12
Using List Comprehension
A more concise approach using list comprehension to extract the first element from each sublist ?
Example
nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:\n", nested_list)
first_items = [item[0] for item in nested_list]
print("First Items from sublists:\n", first_items)
The output of the above code is ?
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: ['Mon', 'Tue', 12]
Using zip and *
The * operator allows us to unpack the sublists and give access to individual elements of the sublist. So in this case we will use * and access the element at index 0 from each element. Then we finally zip the result to get a list of the first element from the sublists ?
Example
nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:\n", nested_list)
first_items = list(list(zip(*nested_list))[0])
print("First Items from sublists:\n", first_items)
The output of the above code is ?
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: ['Mon', 'Tue', 12]
Using itemgetter
The itemgetter(i) constructs a callable that takes an iterable object like dictionary, list, tuple etc. as input, and fetches the i-th element out of it. So we can use this method to get the first items of the list using the map function as follows ?
Example
from operator import itemgetter
nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:\n", nested_list)
first_items = list(map(itemgetter(0), nested_list))
print("First Items from sublists:\n", first_items)
The output of the above code is ?
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: ['Mon', 'Tue', 12]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For loop | High | Good | Simple iteration and printing |
| List comprehension | High | Best | Creating a new list |
| zip with * | Medium | Good | Working with matrix-like data |
| itemgetter | Medium | Good | Functional programming style |
Conclusion
Use list comprehension [item[0] for item in nested_list] for the most readable and efficient solution. For more complex operations, consider itemgetter or the zip(*list) approach when working with matrix-like structures.
