- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("First Items from sublists:\n") for item in Alist: print((item[0]))
Output
Running the above code gives us the following result −
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: Mon Tue 12
Using zip and *
The * 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
Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("\n First Items from sublists:\n") print(list(list(zip(*Alist))[0]))
Output
Running the above code gives us the following result −
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 Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("\n First Items from sublists:\n") print(list(map(itemgetter(0), Alist)))
Output
Running the above code gives us the following result −
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: ['Mon', 'Tue', 12]
- Related Articles
- Get last element of each sublist in Python
- Python Getting sublist element till N
- Find maximum value in each sublist in Python
- Python - Get the Index of first element greater than K
- Get first element with maximum value in list of tuples in Python
- Get the first element of array in JavaScript
- How to get sublist of List in Java?
- How to get First Element of the Tuple in C#?
- Program to find length of longest contiguous sublist with same first letter words in Python
- Get SubList from LinkedList in Java
- Python program to get the indices of each element of one list in another list
- How to get first element in android ConcurrentLinkedDeque?
- Get the element ordered first in Java TreeSet
- Program to find sum of the minimums of each sublist from a list in Python
- How to get the first element of an array in PHP?
