Removing Elements from a List Containing Specific Characters

Removing elements from a list containing specific characters

List comprehensions:

l = ['1', '32', '523', '336']

[ x for x in l if "2" not in x ]

# Returns: ['1', '336']

[ x for x in l if "2" in x ]

# Returns: ['32', '523']
l = ['1', '32', '523', '336']
stringVal = "2"

print(f"{[ x for x in l if stringVal not in x ]}")

# Returns: ['1', '336']

print(f"{[ x for x in l if stringVal in x ]}")

# Returns: ['32', '523']

How to remove elements of a list containing specific patten with python?

Regex searches in python can be done with the re module; specifically, re.search('@\w=', my_string) will not be None if my_string contains an @ and a = separated by a member of \w, i.e. a word character (alphanumerics and _).

I've expanded this to include cases where there's whitespace too, using \s.

import re

listA = ['a', 'b', 'a@b=c', 'a @ b = c', 'a@ =b', 'a@=b' 'a=b@c', 'a@b' ]
listB = [a for a in listA if not re.search('@\s*\w+\s*=', a)]

Update: solution above now using \w+to match multiple word characters instead of just one.

remove elements from a list that begin with a specific character

This does the same as your code:

' '.join(x for x in text.split() if not x.startswith('@'))

Remove elements from python list if they contain special characters

You can use any within list comprehension to filter out lists that have strs using isinstance:

>>> testList = [[30.0, '?', 910.0, 120.],[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0], [7.0,14.0,'?',2.0], ['*', '*', 8.9, 6.4]]
>>> [subL for subL in testList if not any(isinstance(val, str) for val in subL)]
[[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0]]

How can I remove special characters from a list of elements in python?

Use the str.translate() method to apply the same translation table to all strings:

removetable = str.maketrans('', '', '@#%')
out_list = [s.translate(removetable) for s in my_list]

The str.maketrans() static method is a helpful tool to produce the translation map; the first two arguments are empty strings because you are not replacing characters, only removing. The third string holds all characters you want to remove.

Demo:

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans('', '', '@#%')
>>> [s.translate(removetable) for s in my_list]
['on3', 'two', 'three']

Remove words from list containing certain characters

The accepted answer makes use of np.sum which means importing a huge numerical library to perform a simple task that the Python kernel can easily do by itself:

validwords = [w for w in firstcheck if all(c not in w for c in l3)]

Remove element that contains special characters

This should work.Here we are filtering out strings which only contain alphabets and numbers.I believe this can serve your purpose.

 newl = [s for s in list if s.isalnum()]

Find and delete list elements if matching a string

Normally when we perform list comprehension, we build a new list and assign it the same name as the old list. Though this will get the desired result, but this will not remove the old list in place.

To make sure the reference remains the same, you must use this:

>>> stringlist[:] = [x for x in stringlist if "Two" not in x]
>>> stringlist
['elementOne', 'elementThree']

Advantages:
Since it is assigning to a list slice, it will replace the contents with the same Python list object, so the reference remains the same, thereby preventing some bugs if it is being referenced elsewhere.

If you do this below, you will lose the reference to the original list.

>>> stringlist = [x for x in stringlist if "Two" not in x]
>>> stringlist
['elementOne', 'elementThree']

So to preserve the reference, you build the list object and assign it the list slice.

To understand the subtle difference:

Let us take a list a1 containing some elements and assign list a2 equal to a1.

>>> a1 = [1,2,3,4]
>>> a2 = a1

Approach-1:

>>> a1 = [x for x in a1 if x<2]

>>> a1
[1]
>>> a2
[1,2,3,4]

Approach-2:

>>> a1[:] = [x for x in a1 if x<2]

>>> a1
[1]
>>> a2
[1]

Approach-2 actually replaces the contents of the original a1 list whereas Approach-1 does not.

Remove all the strings in an array list containing certain characters

You can use removeIf like this:

list.removeIf(s -> s.contains("book"));


Related Topics



Leave a reply



Submit