Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists.
Input : [[1, 2], [3, 4, 5], [6, 7, 8, 9]] Output : [1, 3, 6] Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']] Output : ['x', 'm', 'a', 'u']
Approach #1 : List comprehension
[1, 3, 6]
Approach #2 : Using zip and unpacking(*) operator
This method uses zip with * or unpacking operator which passes all the items inside the ‘lst’ as arguments to zip function. Thus, all the first element will become the first tuple of the zipped list. Returning the 0th element will thus, solve the purpose.
[1, 3, 6]
Another method of using zip is given below:-
Approach #3 : Using
[1, 3, 6]
- Python | Get last element of each sublist
- Python | Getting sublist element till N
- Python | Multiply each element in a sublist by its index
- Python | Sort a list according to the second element in sublist
- Python - Replace sublist from Initial element
- Python | Maximum Sum Sublist
- Python | Mean of consecutive Sublist
- Python - Incremental Sublist Sum
- Python | Indexing a sublist
- Python | Replace sublist with other in list
- Python | Shift sublist in list
- Python - Check for Sublist in List
- Python | Find maximum value in each sublist
- Python | Split Sublist Strings
- Python | Remove sublists that are present in another sublist
- Python | Convert nested sublist into tuples
- Python | Find the sublist with maximum value in given nested list
- Python - Sublist Maximum in custom sliced List
- Python | Insert Nth element to Kth element in other list
- Python | Slicing list from Kth element to last element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.