Python | Records with Value at K index

In this program, we are given a list of tuples of variable length and two integers an element ele and index K. We need to create a Python program to get records with value at k index.
Submitted by Shivang Yadav, on November 10, 2021

We will find all the tuples from the list which has element ele at index k.

Filtering records is quite an important task. Fields like data science, machine learning often require data cleaning before working with them. And such methods are employed to cleanse data. In this program, we will be discussing filtering tuples based on the element's presence at the given index K.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Getting Records with Value at K index

In this program, we are given a list of tuples and two integer value elements, and index k. We will be creating a python program to get records with value at k index.

Input:
[(5, 7), (4, 8, 3, 1), (5, 8, 0), (1, 2, 3, 4, 5, 7)] 
ele = 8, k = 2

Output:
[(4, 8, 3, 1), (5, 8, 0)]

Method 1:

One method to solve the problem is by using a direct approach where we will traverse the list and check if the given element is present at the specific index k.

# Program to get records with Value 
# at K index using loop,
  
# intializing and printing the tupleList, 
tupList = [(5, 7, 1), (4, 8, 3), (5, 8, 0), (1, 2, 3)]
print("Initial values of the tuple list are " + str(tupList))
  
ele = 8
K = 1

# filtering records with element at k index using loop  
filterList = []
for x, y, z in tupList:
    if y == ele:
        filterList.append((x, y, z))
  
print("The tuples from the list with given element at Kth index is " + str(filterList))

Output:

Initial values of the tuple list are [(5, 7, 1), (4, 8, 3), (5, 8, 0), (1, 2, 3)]
The tuples from the list with given element at Kth index is [(4, 8, 3), (5, 8, 0)]

Method 2:

Another method to solve the problem is by enumeration of indexes and then comparing the element for the given index. To perform the task in a single line of code, we will be using list comprehension.

# Program to get records with Value 
# at K index using loop,
  
# intializing and printing the tupleList, 
tupList = [(5, 7, 1), (4, 8, 3), (5, 8, 0), (1, 2, 3)]
print("Initial values of the tuple list are " + str(tupList))
  
ele = 8
K = 1

# filtering records with element at k index using loop  
filterList = [b for a, b in enumerate(tupList) if b[K] == ele]

print("The tuples from the list with given element at Kth index is " + str(filterList))

Output:

Initial values of the tuple list are [(5, 7, 1), (4, 8, 3), (5, 8, 0), (1, 2, 3)]
The tuples from the list with given element at Kth index is [(4, 8, 3), (5, 8, 0)]

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.