w3resource

Python: Extract the nth element from a given list of tuples

Python List: Exercise - 114 with Solution

Write a Python program to extract the nth element from a given list of tuples.

Visual Presentation:

Python List: Extract the nth element from a given list of tuples.

Sample Solution:

Python Code:

# Define a function 'extract_nth_element' that extracts the nth element from each tuple in a list
def extract_nth_element(test_list, n):
    # Use list comprehension to extract the nth element (index n) from each tuple in the list
    result = [x[n] for x in test_list]
    return result

# Create a list 'students' containing tuples with student names and their exam scores
students = [('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)] 

# Print a message indicating the original list
print("Original list:")
# Print the contents of the 'students' list
print(students)

# Set the value of 'n' to 0
n = 0
# Print a message indicating the extraction of the 0th element from each tuple
print("\nExtract nth element (n =", n, ") from the said list of tuples:")
# Call the 'extract_nth_element' function with 'students' and 'n', then print the result
print(extract_nth_element(students, n))

# Set the value of 'n' to 2
n = 2
# Print a message indicating the extraction of the 2nd element from each tuple
print("\nExtract nth element (n =", n, ") from the said list of tuples:")
# Call the 'extract_nth_element' function with 'students' and 'n', then print the result
print(extract_nth_element(students, n)) 

Sample Output:

Original list:
[('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)]

Extract nth element ( n = 0 ) from the said list of tuples:
['Greyson Fulton', 'Brady Kent', 'Wyatt Knott', 'Beau Turnbull']

Extract nth element ( n = 2 ) from the said list of tuples:
[99, 96, 94, 98]

Flowchart:

Flowchart: Extract the nth element from a given list of tuples.

Python Code Editor:

Previous: Write a Python program to remove duplicate dictionary from a given list.
Next: Write a Python program to check if the elements of a given list are unique or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.