
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Accessing nth element from Python tuples in list
A python list can contain tuples as its elements. In this article we will explore how to access every nth element form the tuples that are present as the elements in the given tuple.
Using index
We can design a for loop to access the elements from the list with the in clause applied for nth index. Then we store the result into a new list.
Example
Alist = [('Mon','3 pm',10),('Tue','12pm',8),('Wed','9 am',8),('Thu','6 am',5)] #Given list print("Given list: ",Alist) # Use index res = [x[1] for x in Alist] print("The 1 st element form each tuple in the list: \n",res)
Output
Running the above code gives us the following result −
Given list: [('Mon', '3 pm', 10), ('Tue', '12pm', 8), ('Wed', '9 am', 8), ('Thu', '6 am', 5)] The 1 st element form each tuple in the list: ['3 pm', '12pm', '9 am', '6 am']
Use itemgetter
The itegetter function from operator module can fetch each item form the given iterable till the end of the iterable is searched. In this program we search for index position 2 from the given list and apply a map function to apply the same function again and again to each result from the result of the itemgetter function. Finally we store the result as a list.
Example
from operator import itemgetter Alist = [('Mon','3 pm',10),('Tue','12pm',8),('Wed','9 am',8),('Thu','6 am',5)] #Given list print("Given list: ",Alist) # Use itemgetter res = list(map(itemgetter(2), Alist)) print("The 1 st element form each tuple in the list: \n",res)
Output
Running the above code gives us the following result −
Given list: [('Mon', '3 pm', 10), ('Tue', '12pm', 8), ('Wed', '9 am', 8), ('Thu', '6 am', 5)] The 1 st element form each tuple in the list: [10, 8, 8, 5]
- Related Articles
- Filter Tuples by Kth element from List in Python
- Rear element extraction from list of tuples records in Python
- Accessing Values of Tuples in Python
- Find the tuples containing the given element from a list of tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Remove Tuples from the List having every element as None in Python
- Python – Remove Tuples from a List having every element as None
- Filter tuples according to list element presence in Python
- Remove tuples from list of tuples if greater than n in Python
- Python | Remove empty tuples from a list
- Python – Extract Kth element of every Nth tuple in List
- Combining tuples in list of tuples in Python
- Remove tuples having duplicate first value from given list of tuples in Python
- Python Program to Select the nth Smallest Element from a List in Expected Linear Time
- Python Program to Select the nth Largest Element from a List in Expected Linear Time
