ADVERTISEMENT
ADVERTISEMENT

Python program to remove tuples from the list having every element as None

Here, we have a list of tuples and we need to remove tuples from the list having every element as None in Python.
Submitted by Shivang Yadav, on July 31, 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 ordered set of values enclosed in square brackets [].

Example:

list = [3 ,1,  5, 7]

Removing tuples from the list having every element as None

We need to traverse all elements of the list and then check if all elements of a tuple of the list are None. if every element of the list is none, remove it from the list otherwise do nothing.

Input:
[(32, 5), (None, None), (None, 6), (6, None), (None, None)]

Output:
[(32, 5), (None, 6), (6, None)]

Python programming language provides more than one way to perform the task.

Method 1:

In this method, we will traverse the list and then using the all() method, check if all values of the tuples are None, remove tuples with all values none. To make the code better we will use list comprehension.

Program:

# Python program to remove tuples from the list 
# where all values are None

# Creating Tuples List and printing values
tupList = [(32, 5), (None, None), (None, 6), (6, None), (None, None)]
print("The List of Tuples before removal : " + str(tupList))

# Updating Values 
updatedTupList = [tup for tup in tupList if not all(vals == None for vals in tup)]

# Printing values 
print("The List of Tuples after removal  : " + str(updatedTupList))

Output:

The List of Tuples before removal : [(32, 5), (None, None), (None, 6), (6, None), (None, None)]
The List of Tuples after removal  : [(32, 5), (None, 6), (6, None)]

Method 2:

Another method to solve the problem is using some built-in functions instead of comprehension. We will use the filter() method to filter off values which will be calculated using lambda function checking if all values of the tuple are None using all() method.

Program:

# Python program to remove tuples from list 
# where all values are None

# Creating Tuples List and printing values
tupList = [(32, 5), (None, None), (None, 6), (6, None), (None, None)]
print("The List of Tuples before removal : " + str(tupList))

# Updating Values 
updatedTupList = list(filter(lambda tup : not all(vals == None for vals in tup), tupList))
  
# Printing values 
print("The List of Tuples after removal  : " + str(updatedTupList))

Output:

The List of Tuples before removal : [(32, 5), (None, None), (None, 6), (6, None), (None, None)]
The List of Tuples after removal  : [(32, 5), (None, 6), (6, None)]

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.