How to Remove All String Values from List in Python?

By Hardik Savani October 30, 2023 Category : Python

Hi Developer,

In this quick guide, we will teach you how to remove all string values from a list in python. This tutorial will give you a simple example of python list remove string values. I explained simply about python list remove string values. if you want to see an example of how to remove string from list in python then you are in the right place.

If you have list in python with numeric and string values in list and you want to get only string values from 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:

You can use these examples with python3 (Python 3) version.

let's see below a simple example with output:

Example 1:

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]

Example 2:

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 val.isdigit()]
  
print(newList)

Output:

['1', '2', '3', '4', '5']

I hope it can help you...

Tags :
Shares