How To Get The First Element Of Each Tuple In A List In Python

Get the first element of each tuple in a list in Python

To get the first element of each tuple in a list in Python, you must have knowledge of Tuple and List. So, Let’s discover the differences and similarities between Tuple and List in Python.

Tuple and List in Python

Tuple and List are both iterable datatype. But the elements in Tuple are immutable, unlike List, which can change the value of the elements after being assigned. In addition, Tuples are created with round brackets ( ) And Lists are created with square brackets [ ].

Get the first element of each tuple in a list in Python

Method 1: Using the For Loop

Syntax:

for value in sequence:
    #loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a List, a Tuple, a Set, or a string.

Using the For Loop is the easiest way to solve this problem.

We use For Loop to iterate each Tuple in List and get the first element of Tuple by using the index 0.

Like this:

tuples = [("learn", "python"), ("share","knowledge"), ("it", "works")]
firstTuples = []

for tuple in tuples:
  firstTuples.append(tuple[0])

print(firstTuples)

Output:

['learn', 'share', 'it']

Method 2: Using the List Comprehensions

Syntax:

list = [expression for element in iterable]

Parameters:

  • element: the element of iterable on each iteration.
  • expression: any valid expression that returns a value.
  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.

List comprehension is an easy way to create a list based on an iterable object. The return value of List Comprehensions is a list that contains the value of the expression. Just loop over the List and place the tuple[0] on the expression. We can get all the first elements of Tuple in the List.

We can use List Comprehensions to solve the problem quickly. Like this:

tuples = [("learn", "python"), ("share", "knowledge"), ("it", "works")]
firstTuples = [tuple[0] for tuple in tuples]
print(firstTuples)

Output:

['learn', 'share', 'it']

Method 3: Using the map() functions

Syntax:

map(function, iterable)

Parameters:

  • function: the function that is called on each iteration to execute for each item.
  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.

First, we define a getFirst() function, which gets the first element of a given value. Then we pass the getFirst() function and a tuple list into the map() function. The return value of the map() is a map object. Just convert it to a List, and we have all the first elements of Tuple in a List.

tuples = [("learn", "python"), ("share", "knowledge"), ("it", "works")]

def getFirst(x):
  return x[0]

firstTuples = map(getFirst, tuples)
print(list(firstTuples))

Output:

['learn', 'share', 'it']

Summary

There are many ways to get the first element of each Tuple in a list in Python. However, the three methods mentioned in this article are the most straightforward approaches. Hopefully, they will help you. Leave a comment below if you have any problems.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *