Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Remove duplicate tuples from list of tuples
Next article icon

Python - Remove Tuples from the List having every element as None

Last Updated : 15 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Tuple list, remove all tuples with all None values.

Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]  Output : [(None, 2), (3, 4), (12, 3)]  Explanation : All None tuples are removed.
Input : test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]  Output : [(3, 4), (12, 3)]  Explanation : All None tuples are removed. 

Method #1 : Using all() + list comprehension

In this, we use all() to check for all None values for discarding and list comprehension does task of iteration.

Python3
# Python3 code to demonstrate working of  # Remove None Tuples from List # Using all() + list comprehension  # initializing list test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]  # printing original list print("The original list is : " + str(test_list))  # negating result for discarding all None Tuples res = [sub for sub in test_list if not all(ele == None for ele in sub)]  # printing result  print("Removed None Tuples : " + str(res)) 

Output
The original list is : [(None, 2), (None, None), (3, 4), (12, 3), (None,)] Removed None Tuples : [(None, 2), (3, 4), (12, 3)]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m), where m is the number of non-None tuples in the input list.

Method #2 : Using filter() + lambda + all()

In this method, task of filtering None tuples is done using filter() and lambda function to provide None checking functionality using all().

Python3
# Python3 code to demonstrate working of  # Remove None Tuples from List # Using filter() + lambda + all()  # initializing list test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]  # printing original list print("The original list is : " + str(test_list))  # filter() + lambda to drive logic of discarding tuples res = list(filter(lambda sub : not all(ele == None for ele in sub), test_list))  # printing result  print("Removed None Tuples : " + str(res)) 

Output
The original list is : [(None, 2), (None, None), (3, 4), (12, 3), (None,)] Removed None Tuples : [(None, 2), (3, 4), (12, 3)]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n)

Method #3: Using count() method

If count of None in each tuple equals to length of tuple, then the entire tuple elements are None.Used this in if condition and appended other tuples to output list

Python3
# Python3 code to demonstrate working of # Remove None Tuples from List  # initializing list test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]   # printing original list print("The original list is : " + str(test_list))  # negating result for discarding all None Tuples res=[] for i in test_list:     if not(i.count(None)== len(i)):         res.append(i)  # printing result print("Removed None Tuples : " + str(res)) 

Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)] Removed None Tuples : [(3, 4), (12, 3)]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using enumerate function

Python3
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]   res = [sub for i,sub in enumerate(test_list) if not all(ele == None for ele in sub)] print(res) 

Output
[(3, 4), (12, 3)]

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  enumerate function performs n number of operations.
Auxiliary Space: O(n), extra space is required 

Method #5 : Using operator.countOf()

Python3
# Python3 code to demonstrate working of # Remove None Tuples from List  # initializing list test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]  # printing original list print("The original list is : " + str(test_list))  # negating result for discarding all None Tuples res=[] import operator for i in test_list:     if not(operator.countOf(i,None)== len(i)):         res.append(i)  # printing result print("Removed None Tuples : " + str(res)) 

Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)] Removed None Tuples : [(3, 4), (12, 3)]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method 6: Using a for loop and slicing

In this method, we use a for loop to iterate through the list and remove the None tuples by slicing the list. 

Python3
# Python3 code to demonstrate working of # Remove None Tuples from List  # initializing list test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]   # printing original list print("The original list is : " + str(test_list))  # using for loop and slicing to remove None Tuples res = [] for i in range(len(test_list)):     if None not in test_list[i]:         res.append(test_list[i])  # printing result print("Removed None Tuples : " + str(res)) 

Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)] Removed None Tuples : [(3, 4), (12, 3)]

Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n), where n is the length of the list (to store the result list)

Method 7: Using recursion

Step-by-step approach:

  • Define the original list
  • Define a recursive function that takes the original list and an empty list as arguments
  • The function should check if the original list is empty
  • If it is, return the empty list
  • Otherwise, check if the first element of the list is a tuple containing None values
  • If it is, call the function recursively with the remaining elements of the list and the empty list
  • Otherwise, append the first element of the list to the empty list and call the function recursively with the remaining elements of the list and the updated list
  • Call the recursive function with the original list and an empty list as arguments and assign the result to a new variabl
Python3
# Define the original list test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]  # Define the recursive function def remove_none_tuples(lst, res):     if not lst:         return res     elif None in lst[0]:         return remove_none_tuples(lst[1:], res)     else:         res.append(lst[0])         return remove_none_tuples(lst[1:], res)  # Call the recursive function with the original list and an empty list new_list = remove_none_tuples(test_list, [])  # Print the new list print("Removed None Tuples : " + str(new_list)) 

Output
Removed None Tuples : [(3, 4), (12, 3)]

Time complexity: O(n), where n is the length of the original list
Auxiliary space: O(n), where n is the length of the original list 


Next Article
Python | Remove duplicate tuples from list of tuples
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • Python | Remove given element from the list
    Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
    7 min read
  • Python Program to remove a specific digit from every element of the list
    Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
    7 min read
  • Python | Remove trailing empty elements from given list
    When working with lists in Python, it's common to encounter lists with extra None elements at the end. These trailing None values can cause issues in our programs . We can remove these trailing None elements using simple methods like slicing, loops, or filter. Using List Slicing Slicing is a very ef
    3 min read
  • Python | Remove all strings from a list of tuples
    Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
    8 min read
  • Python | Remove duplicate tuples from list of tuples
    Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
    5 min read
  • Python - Filter Tuples by Kth element from List
    Given a list of tuples, filter by Kth element presence in List. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4, 2, 3, 10], K = 2 Output : [('is', 4, 3)] Explanation : 3 is 2nd element and present in list, hence filtered tuple. Input : test_list = [("GFg", 5, 9),
    5 min read
  • Removing Tuples from a List by First Element Value - Python
    In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desire
    3 min read
  • Python | Remove tuples having duplicate first value from given list of tuples
    Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), (
    7 min read
  • Python - Remove given character from first element of Tuple
    Given a Tuple list, remove K character from 1st element of the Tuple being String. Input : test_list = [("GF$g!", 5), ("!i$s", 4), ("best!$", 10)], K = '$' Output : [('GFg!', 5), ('!is', 4), ('best!', 10)] Explanation : First element's strings K value removed. Input : test_list = [("GF$g!", 5), ("be
    5 min read
  • Remove common elements from two list in Python
    When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences