In the previous article, we have discussed Python Program for Modulo of Tuple Elements
Tuple in Python:
A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.
Given some random number of tuples in a list and the task is to get the first element of each tuple in a given tuple list.
For example
let the tuple list = [(1, 2),(3, 4)]
The first element of tuples in the given list = [1, 3]
Examples:
Example1:
Input:
Given Tuple list = [(1, 2), (3, 4)]
Output:
The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]
Example2:
Input:
Given Tuple list = [('p', 'q', 'r'), ('s', 't'), ('u', 'v')]Output:
The first element of the each tuple in a given tuplelist [('p', 'q', 'r'), ('s', 't'), ('u', 'v')] = ['p', 's', 'u']Program to get the First Element of each Tuple in a List in Python
Below are the ways to get the first element of each tuple in a given tuple list:
Method #1: Using For Loop (Static Input)
Approach:
- Give the tuple list as static input and store it in a variable.
- Take an empty new list to store the first element of each tuple in a given tuple list.
- Loop in the given input tuple list using the for loop.
- Inside the loop, append the first element of each tuple in a given tuple list to the above initialized new list using the append() function.
- Print the new list to get the first element of each tuple in a given tuple list.
- The Exit of the program.
Below is the implementation:
# Give the tuple list as static input and store it in a variable.
gvn_tupl_lst = [(1, 2), (3, 4)]
# Take an empty new list to store the first element of each tuple in a given tuple list.
fst_tupl_elemnts = []
# Loop in the given input tuple list using the for loop.
for elemnt in gvn_tupl_lst:
# Inside the loop, append the first element of each tuple in a given tuple list to
# the above initialized new list using the append() function.
fst_tupl_elemnts.append(elemnt[0])
# Print the new list to get the first element of each tuple in a given tuple list.
print("The first element of the each tuple in a given tuplelist",
gvn_tupl_lst, "=", fst_tupl_elemnts)
Output:
The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]
Method #2: Using For loop (User Input)
Approach:
- Give the number of tuples in a tuple list as user input using the int(input()) Â function and store it in a variable.
- Take an empty list say “gvn_tupl_lst” and store it in another variable.
- Iterate in the above-given number of times using the for loop.
- Inside the loop, enter the tuple elements using tuple(),map(),input(),and split() functions and Store it in a variable.
- Append the above elements to the “gvn_tupl_lst”.
- Take an empty new list to store the first element of each tuple in a given tuple list.
- Loop in the given input tuple list using the for loop.
- Inside the loop, append the first element of each tuple in a given tuple list to the above initialized new list using the append() function.
- Print the new list to get the first element of each tuple in a given tuple list.
- The Exit of the program.
Below is the implementation:
# Give the number of tuples in a tuple list as user input using the int(input())
# function and store it in a variable
numtuples=int(input('Enter some random number of tuples = '))
#Take an empty list say "gvn_tupl_lst" and store it in another variable.
gvn_tupl_lst=[]
#Iterate in the above-given number of times using the for loop.
for t in range(numtuples):
# Inside the loop, enter the tuple elements using tuple(),map(),input(),and split() functions
# and Store it in a variable.
tupleelemts=tuple(map(int,input('Enter random tuple elements = ').split()))
# Append the above elements to the "gvn_tupl_lst".
gvn_tupl_lst.append(tupleelemts)
# Take an empty new list to store the first element of each tuple in a given tuple list.
fst_tupl_elemnts = []
# Loop in the given input tuple list using the for loop.
for elemnt in gvn_tupl_lst:
# Inside the loop, append the first element of each tuple in a given tuple list to
# the above initialized new list using the append() function.
fst_tupl_elemnts.append(elemnt[0])
# Print the new list to get the first element of each tuple in a given tuple list.
print("The first element of the each tuple in a given tuplelist",
gvn_tupl_lst, "=", fst_tupl_elemnts)
Output:
Enter some random number of tuples = 2 Enter random tuple elements = 7 8 9 Enter random tuple elements = 4 5 6 The first element of the each tuple in a given tuplelist [(7, 8, 9), (4, 5, 6)] = [7, 4]
If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.
