- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- Removing duplicates from tuple in Python
- Common words among tuple strings in Python
- 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?
- How can I convert Python strings into tuple?
- How can I create a Python tuple of Unicode strings?
- Remove nested records from tuple in Python
- Finding unique elements from Tuple in Python
- Removing nth character from a string in Python program
- How can I subtract tuple of tuples from a tuple in Python?
- Extract digits from Tuple list Python
- Program to find maximum score from removing stones in Python
- Program to count maximum score from removing substrings in Python