ADVERTISEMENT
ADVERTISEMENT

Python program to find tuples with positive elements in list of tuples

Here, we have a list of tuples and we need to find all the tuples from the list with all positive elements in the tuple.
Submitted by Shivang Yadav, on September 02, 2021

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)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets [].

Example:

list = [3 ,1,  5, 7]

Finding Tuples with positive elements in list of Tuples

We will be traversing all the tuples of the list and then check if all elements of each tuple.

Input:
[(4, 1), (9, -1), (5, -7)]

Output:
[(4, 1)]

Method 1:

To find all the tuples of the list that consist of only positive values. For performing this task, we will use the filter() method and check the condition that all values are positive using the lambda function and all().

# Python program to find Tuples 
# with positive elements in list 

# Creating a new list and 
tupList = [(4, 1), (9, -1), (5, -7)]
print("The original list is : " + str(tupList))

# Finding Tuples with Positive elements in list 
positiveList = list(filter(lambda tup: all(val >= 0 for val in tup), tupList))

# Printing filtered tuple 
print("Tuples with positive values : " + str(positiveList))

Output:

The original list is : [(4, 1), (9, -1), (5, -7)]
Tuples with positive values : [(4, 1)]

Method 2:

Another method to solve the problem is using the list comprehension technique to find the tuples from the list which consist of only positive elements. We will use all methods to test the condition on all elements of the tuple.

# Python program to find Tuples 
# with positive elements in list 

# Creating a new list and 
tupList = [(4, 1), (9, -1), (5, -7)]
print("The original list is : " + str(tupList))

# Finding Tuples with Positive elements in list 
positiveList = [tup for tup in tupList if all(val >= 0 for val in tup)]

# Printing filtered tuple 
print("Tuples with positive values : " + str(positiveList))

Output:

The original list is : [(4, 1), (9, -1), (5, -7)]
Tuples with positive values : [(4, 1)]

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.