Q:

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

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to assign frequency to tuples... >>
<< Python program to find tuples from list which have...