- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing strings from tuple in Python
When it is required to remove the strings froma tuple, the list comprehension and the 'type' method can be used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
The 'type' method returns the class of the iterable passed to it as an argument.
Below is a demonstration for the same −
my_list = [('Hi', 45, 67), ('There', 45, 32), ('Jane', 59, 13)] print("The list is : ") print(my_list) my_result = [tuple([j for j in i if type(j) != str]) for i in my_list] print("The list of tuple after removing the string is : ") print(my_result)
The list is : [('Hi', 45, 67), ('There', 45, 32), ('Jane', 59, 13)] The list of tuple after removing the string is : [(45, 67), (45, 32), (59, 13)]
- A list of tuple is defined, and is displayed on the console.
- It is iterated over, using list comprehension.
- It is checked to see for not being a string.
- It is then converted to a tuple, and then to a list again.
- This operation's data is stored in a variable.
- This variable is the output that is displayed on the console.
- Removing duplicates from tuple in Python
- Common words among tuple strings in Python
- How can I convert Python strings into tuple?
- Get first index values in tuple of strings in Python
- How to create a tuple from a string and a list of strings in Python?
- Extract digits from Tuple list Python
- Remove nested records from tuple in Python
- Removing nth character from a string in Python program
- How can I create a Python tuple of Unicode strings?
- How can I subtract tuple of tuples from a tuple in Python?
- Program to find maximum score from removing stones in Python
- Program to count maximum score from removing substrings in Python
- Python program for removing nth character from a string