Python remove non string from list

I'm looking to 'clean' a list by excluding any items which contain characters other than 0-9, and wondering if there's a more efficient way than e.g.

import re
invalid = re.compile('[^0-9]')    
ls = ['1a', 'b3', '1']
cleaned = [i for i in ls if not invalid.search(i)]
print cleaned
>> ['1']

As I'm going to be operating on large-ish lists (5k items) of long strings (15 chars).

asked May 17, 2011 at 11:26

Anything wrong with the string method isdigit ?

>>> ls = ['1a', 'b3', '1']
>>> cleaned = [ x for x in ls if x.isdigit() ]
>>> cleaned
['1']
>>>

answered May 17, 2011 at 11:29

MattHMattH

36k11 gold badges81 silver badges84 bronze badges

2

You can use isnumeric function. It checks whether the string consists of only numeric characters. This method is present only on unicode objects. It won't work with integer or float values

myList = ['text', 'another text', '1', '2.980', '3']
output = [ a for a in myList if a.isnumeric() ]
print( output )      
# Output is : ['1', '3']

Ref: https://www.tutorialspoint.com/python/string_isnumeric.htm

answered Nov 11, 2017 at 20:22

Python remove non string from list

Remove non-numeric characters except for "." in Python #

Use the re.sub() method to remove all non-numeric characters except for dot . from a string, e.g. result = re.sub(r'[^0-9.]', '', my_str). The re.sub() method will remove all non-numeric characters from the string by replacing them with empty strings.

Copied!

import re my_str = 'a3.1b4c' result = re.sub(r'[^0-9.]', '', my_str) print(result) # 👉️ '3.14'

If you're looking to avoid using regular expressions, scroll down to the next subheading.

We used the re.sub() method to remove all non-numeric characters except for dot from a string.

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub() method is a regular expression.

The square brackets [] are used to indicate a set of characters.

If the first character of the set is a caret ^, all characters that are not in the set will be matched.

In other words, our set matches any character that is not a digit in the range 0-9 or a dot.

The second argument we passed to the re.sub() method is the replacement for each match.

Copied!

import re my_str = 'a3.1b4c' result = re.sub(r'[^0-9.]', '', my_str) print(result) # 👉️ '3.14'

We want to remove all non-numeric characters or dots, so we replace each with an empty string.

There is also a shorthand for the 0-9 range.

Copied!

import re my_str = 'a3.1b4c' result = re.sub(r'[^\d.]', '', my_str) print(result) # 👉️ '3.14'

The \d character matches any Unicode decimal digit. This includes [0-9], and many other digit characters.

Remove all non-numeric characters except "." from String using join() #

To remove all non-numeric characters except for "." from a string:

  1. Use a generator expression to iterate over the string.
  2. Check if each character is a digit or a dot and return the result.
  3. Use the str.join() method to join the characters that pass the test.

Copied!

my_str = 'a3.1b4c' result = ''.join(char for char in my_str if char in '123456789.') print(result) # 👉️ '3.14'

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current character is a digit or a dot and return the result.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

The generator object only contains the digits and dots from the string.

Copied!

my_str = 'a3.1b4c' # 👇️ ['3', '.', '1', '4'] print(list(char for char in my_str if char in '123456789.'))

The last step is to join the digits and the dot into a string.

Copied!

my_str = 'a3.1b4c' result = ''.join(char for char in my_str if char in '123456789.') print(result) # 👉️ '3.14'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

For our purposes, we called the join() method on an empty string to join the digits and the dot without a separator.

How do I remove a non string from a list in Python?

Use the filter() Function to Remove All Non-Alphanumeric Characters in Python String. The filter() function is used to construct an iterator from components of the iterable object and filters the object's elements using a function.

How do you remove one thing from a list in Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do I remove a specific string from a list?

Use list. remove() to remove a string from a list. Call list. remove(x) to remove the first occurrence of x in the list.

How do you remove certain strings from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.