Remove All String Values from List in Python Tutorial Example

11-May-2023

.

Admin

Remove All String Values from List in Python Tutorial Example

Hi Dev,

Here, I will show you how to works remove all string values from the list in the Python tutorial example. We will use how to remove all string values from a list in Python. if you want to see an example of a Python list removing string values then you are in the right place. This article goes into detail on Python list remove string values.

If you have a list in Python with numeric and string values in the list and you want to get only string values from the list in Python, then there are several ways to do that. I will give you simple two examples here using comprehension with isinstance() and isdigit() functions. so, let's see the following examples:

Example :


main.py

# Create New List with Item

myList = ["a", 1, 2, "b", "c", 4, 5, "d"]

# Python list remove string values

newList = [val for val in myList if isinstance(val, (int, float))]

print(newList)

Output:

[1, 2, 3, 4, 5]

#Python