ADVERTISEMENT
ADVERTISEMENT

Python program to filter tuples according to list element

In this problem, we are given a list of tuples and a list consisting of integer values. Our task is to create a Python program to filter tuples according to list elements.
Submitted by Shivang Yadav, on January 14, 2022

Working with numbers and data in programming is complex. Sometimes it requires the extraction of data chunks after performing some operation. Summation, selective extraction, mean, etc., are some common operations. And as a programmer, you should know how these operations are performed. Here is a program in Python using which we can perform the filtration of tuples of the list of tuples based on the elements of the given list.

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)

Filtering tuples according to list element

We are given a list of tuples and a list consisting of integer elements. We need to create a Python program to filter tuples that contain elements that are present in the list.

Input:
[(2, 9) ,(5, 6), (1, 3), (4, 8)]
[2, 3]

Output:
[(2, 9), (1, 3)]

We will iterate over all elements of the list of tuples and check if their elements are present in the list. And filter off all values the are not present in the list.

In Python, we have multiple methods and ways to perform this task.

Method 1:

One direct method is to use list comprehension to perform the iteration of the tuple of the list and check for the presence of elements in the list and filter based on it.

# Python program to filter tuples according 
# to list element

# Initializing and printing list 
# of tuple and filter list
tupList = [(1, 4, 6), (5, 8), (2, 9), (1, 10)]
filterList = [6, 10]

print("The elements of List of Tuple are " + str(tupList))
print("The elements of filter list are " + str(filterList))

# Filtering tuple of tuple list
filteredTupList = [tuples for tuples in tupList if any(value in tuples for value in filterList)]

print("The filter tuples from the list are " + str(filteredTupList))

Output:

The elements of List of Tuple are [(1, 4, 6), (5, 8), (2, 9), (1, 10)]
The elements of filter list are [6, 10]
The filter tuples from the list are [(1, 4, 6), (1, 10)]

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.