Sometimes, we want to extract first item of each sublist with Python.
In this article, we’ll look at how to extract first item of each sublist with Python.
How to extract first item of each sublist with Python?
To extract first item of each sublist with Python, we can use list comprehension.
For instance, we write
lst = [["a", "b", "c"], [1, 2, 3], ["x", "y", "z"]]
lst2 = [item[0] for item in lst]
to create a list lst2
that has the first item from each nested list in lst
in the list.
We use item[0]
to get the item.
Conclusion
To extract first item of each sublist with Python, we can use list comprehension.