How to Get the First Element in List of Tuples in Python?


Tuples are immutable data types in Python that can hold heterogeneous data types. List, on the other hand, are multiple data types that can hold heterogeneous data. In this article, we will explore various methods to retrieve the first element from a list of tuples using function−based components. We will explore several methods like loops, list comprehension, zip method, etc.

Using Loop Method

Loops are common statements in almost any Programming language. We can use the loop statement along with the indexing property of the iterable objects of Python to access the first elements of the iterable objects. Since, in our case, we want to access only the first element, we can use the index number 0 in each iteration.

Example

In the following example, we have created a function that returns a list as input and returns all the first elements of the list in the form of a list data type. Under the function, we created an empty Tuple, and under each iteration with the list, we appended the first element of the tuple elements to the initialized list. Later we created a tuple and called the function to test the results.

def get_first_element_using_loop(tuples_list):
    first_elements = []
    for tuple in tuples_list:
        first_element = tuple[0]
        first_elements.append(first_element)
    return first_elements
t = [
    ('Apple', 5, True),
    ('Banana', 3, False),
    ('Orange', 2, True),
    ('Grape', 4, False),
    ('Mango', 6, True)
]
print(f"The first elements of the list are: {get_first_element_using_loop(t)}")

Output

The first elements of the list are: ['Apple', 'Banana', 'Orange', 'Grape', 'Mango']

Using List Comprehension

List comprehension is a very convenient way to combine expressions and statements in Python and use them to create elements in a list. For our need, we can use the indexing method and the for loop as the conditional expression to create the elements in the list.

Example

In the following example, we have created a function that accepts the list and returns a list containing all the first elements of the Tuples. We used the list comprehension technique to create the list elements named first_elements.

def get_first_element_using_comprehension(tuples_list):
    first_elements = [tuple[0] for tuple in tuples_list]
    return first_elements

t = [
    ('Apple', 5, True),
    ('Banana', 3, False),
    ('Orange', 2, True),
    ('Grape', 4, False),
    ('Mango', 6, True)
]
print(f"The first elements of the list are: {get_first_element_using_comprehension(t)}")

Output

The first elements of the list are: ['Apple', 'Banana', 'Orange', 'Grape', 'Mango']

Using Map Method

Map is another important in−built method of Python. The map method applies a function to all the elements of an iterable object. It takes two parameters, namely the function name and the iterable object. We can use the lambda function and the map method to access the first element of the tuples in the list.

Example

In the following example, we have created a function named get_first_element_using_map which takes the list consisting of tuple elements and returns all the first elements of the tuple element of the list. We used the map method to apply a lambda function to each list element.

def get_first_element_using_map(tuples_list):
    first_elements = list(map(lambda x: x[0], tuples_list))
    return first_elements


t = [
    ('Lemon', 3, False),
    ('Strawberry', 7, True),
    ('Watermelon', 1, True),
    ('Pineapple', 4, False),
    ('Kiwi', 2, True)
]

print(f"The first elements of the list are: {get_first_element_using_map(t)}")

Output

The first elements of the list are: ['Lemon', 'Strawberry', 'Watermelon', 'Pineapple', 'Kiwi']

Using Unpacking Technique

This is a tricky way to extract all the first elements. First, we can use the for loop; next, we can unpack the first one by specifying it some name. We can also specify the other elements with *. We can use list comprehension to append all the elements into a list.

Syntax

for first_element, *_ in iterable:
    # other codes

Here we have assigned the first element to be first_element. Note that you can assign any name to it. Next, “* _ “ specifies we have other elements, but we are not interested in those. The “iterable” is any iterable object like Tuple List etc.

Example

In the following code, we have used the list comprehension technique and the unpacking technique of Python. Here we have named the first element of each tuple element of the list to be first_element and appended it to the list. The function we created is a non−void function that returns the list generated.

def get_first_element_using_unpacking(tuples_list):
    first_elements = [first_element for first_element, *_ in tuples_list]
    return first_elements

t = [
    ('Lemon', 3, False),
    ('Strawberry', 7, True),
    ('Watermelon', 1, True),
    ('Pineapple', 4, False),
    ('Kiwi', 2, True)
]

print(f"The first elements of the list are: {get_first_element_using_unpacking(t)}")

Output

The first elements of the list are: ['Lemon', 'Strawberry', 'Watermelon', 'Pineapple', 'Kiwi']

Using Zip Method

The zip() function in Python is a built−in function that allows you to combine multiple iterable (such as lists, tuples, or strings) into a single iterator of tuples. Each tuple generated by zip() contains elements from corresponding positions in the input iterable and the element itself. Since the return value is a list consisting of the elements of the tuple, we can use the indexing method to access the first elements of the lists.

Example

In the following code, we created a non−void function that takes the list as the argument and returns the first element in the list of Tuples. If you print the result of list(zip(*tuples_list)) you will get the result to be something like this:

[('Lemon', 'Strawberry', 'Watermelon', 'Pineapple', 'Kiwi'), (3, 7, 1, 4, 2), (False, True, True, False, True)]

Since we want only the first element in the list, we used the indexing method and set index=0.

def get_first_element_using_unpacking(tuples_list):
    return list(zip(*tuples_list))[0]

t = [
    ('Lemon', 3, False),
    ('Strawberry', 7, True),
    ('Watermelon', 1, True),
    ('Pineapple', 4, False),
    ('Kiwi', 2, True)
]

print(f"The first elements of the list are: {get_first_element_using_unpacking(t)}")

Output

The first elements of the list are: ('Lemon', 'Strawberry', 'Watermelon', 'Pineapple', 'Kiwi')

Conclusion

In this article, we have understood how to get the first element in the list of Tuples in Python. Several methods are available in Python, like the loop method, list comprehension, unpacking, etc., to perform the task. The answer to which method is best depends on the usage and other factors. We strongly recommend that readers try these concepts to have more understanding of the topics.

Updated on: 18-Jul-2023

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements