Remove all strings from a list of tuples in Python


In this article, the user will understand how to remove all strings from a list of tuples in Python. When working with a list of tuples in Python, it's common to come across circumstances where you wish to expel any strings shown within the tuples. Evacuating strings from a list of tuples can be achieved utilizing different approaches. In this article, we are going investigate three distinctive strategies to achieve this task. These methods incorporate utilizing list comprehension, the filter() method with a lambda work, and a for loop with tuple unloading.

Approach 1: Utilizing List Comprehension

One of the foremost brief and exquisite ways to expel strings from a list of tuples is by utilizing list comprehension. The concept of list comprehension permits us to form an unused list by emphasizing an existing list and applying a condition.

Algorithm

  • Step 1 − Define a function named remove_strings_from_tuples() that contains one argument in the function definition. Make an empty list to store the sifted tuples.

  • Step 2 − Repeat each tuple within the unique list.

  • Step 3 − Check each element within the tuple utilizing another emphasis.

  • Step 4 − In the next line, if an element isn't a string, include it in the filtered_ tuple.

  • Step 5 − Add the filtered_tuple to the filtered_list.

  • Step 6 − Return the filtered_list.

Example

# create a user-defined function
def remove_strings_from_tuples(tuples_list):
   filtered_list = []
   for tuple_item in tuples_list:
      filtered_tuple = [element for element in tuple_item if not isinstance(element, str)]
      filtered_list.append(tuple(filtered_tuple))
   return filtered_list

#initialize the list of tuples
tuples_list = [(1, 'apple', 3.14), ('orange', 5, 'banana'), (7, 'grape', 'kiwi')]
result = remove_strings_from_tuples(tuples_list)
print(result)

Output

[(1, 3.14), (5,), (7,)]

Approach 2: Using Filter and Lambda Functions

Another approach to evacuate strings from a list of tuples is by utilizing the filter() work in combination with a lambda. The filter() function permits us to specifically incorporate or avoid components from an iterable based on a condition indicated by the lambda work.

Algorithm

  • Step 1 − Creation of a function named remove_strings_from_tuples() that contains one argument.

  • Step 2 − Characterize a lambda work that checks whether an element could be a string.

  • Step 3 − Utilize the filter() method to apply the lambda work to each tuple within the list.

  • Step 4 − Change over the result back into a list of tuples.

Example

#Define a function
def remove_strings_from_tuples(tuples_list):
   filtered_list = list(filter(lambda t: not any(isinstance(element, str) for element in t), tuples_list))
   return filtered_list

tuples_list = [(1, 'apple', 3.14), ('orange', 5, 'banana'), (7, 'grape', 'kiwi')]
#Invoke function and pass its value to the result variable
result = remove_strings_from_tuples(tuples_list)
print(result)

Output

[]

Approach 3: Using a for Loop and Tuple Unpacking

The ultimate approach we are going to investigate includes employing a for-loop and tuple unloading procedure to expel strings from the tuples. This approach is clear and can be valuable on the off chance that you favor a more unequivocal coding fashion.

Algorithm

  • Step 1 − Creation of the function named remove_strings_from_tuples(). Make an empty list to store the tuples.

  • Step 2 − Use for loop and repeat each tuple within the unique list.

  • Step 3 − Create another list to store the non-string components.

  • Step 4 − Repeat each element within the tuple.

  • Step 5 − Check if the element isn't a string, and if so, add it to the non-string list. Change over the non-string list back into a tuple. Add the tuple to the filtered_list. Return to the filtered_list.

Example

def remove_strings_from_tuples(tuples_list):
   #create an empty list
   filtered_list = []
   #Use for loop to traverse the items
   for tuple_item in tuples_list:
      non_string_elements = []
      for element in tuple_item:
         if not isinstance(element, str):
            non_string_elements.append(element)
      filtered_tuple = tuple(non_string_elements)
      filtered_list.append(filtered_tuple)
   return filtered_list

tuples_list = [(1, 'apple', 3.14), ('orange', 5, 'banana'), (7, 'grape', 'kiwi')]
result = remove_strings_from_tuples(tuples_list)
print(result)

Output

[(1, 3.14), (5,), (7,)]

Conclusion

Keep in mind to consider the characteristics of your information and the execution suggestions of each approach. List comprehension is frequently the foremost brief and proficient choice, but it may not be reasonable for amazingly expansive records. The filter() work with a lambda work offers adaptability and meaningfulness, whereas the for loop with tuple unloading gives a more explicit coding fashion.

Updated on: 29-Aug-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements