
- 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
Python – Extract Kth element of every Nth tuple in List
When it is required to extract ‘K’th element of every ‘N’th tuple in a list, a simple iteration and the ‘append’ method is used.
Example
Below is a demonstration of the same −
my_list = [(54, 51, 23), (73, 24, 47), (24, 33, 72), (64, 27, 18), (63, 24, 67), (12, 25, 77), (31, 39, 80),(33, 55, 78)] print("The list is :") print(my_list) K = 1 print("The value of K is :") print(K) N = 3 print("The value of N is :") print(N) my_result = [] for index in range(0, len(my_list), N): my_result.append(my_list[index][K]) print("The result is :") print(my_result)
Output
The list is : [(54, 51, 23), (73, 24, 47), (24, 33, 72), (64, 27, 18), (63, 24, 67), (12, 25, 77), (31, 39, 80), (33, 55, 78)] The value of K is : 1 The value of N is : 3 The result is : [51, 27, 39]
Explanation
A list of tuple is defined and is displayed on the console.
The values for K and N are defined and are displayed on the console.
An empty list is defined.
The list is iterated over, and the element at a specific index at ‘K’ is appended to the empty list
This is the output that is displayed on the console.
- Related Articles
- Kth Column Product in Tuple List in Python
- Python – Append List every Nth index
- Extract digits from Tuple list Python
- Closest Pair to Kth index element in Tuple using Python
- Get maximum of Nth column from tuple list in Python
- Consecutive Nth column Difference in Tuple List using Python
- Filter Tuples by Kth element from List in Python
- Kth smallest element after every insertion in C++
- Maximum element in tuple list in Python
- Accessing nth element from Python tuples in list
- Python program to covert tuple into list by adding the given string after every element
- Python program to convert tuple into list by adding the given string after every element
- Update each element in tuple list in Python
- Python – Cross Join every Kth segment
- Finding sum of every nth element of array in JavaScript

Advertisements