0

I'm still a noob when it comes to python. I have this list that contains strings. My problem is to remove ''(empty string) from the list.

I've tried to use .remove() in python but it still does not work.

For example, I have a list that contains next data:

a = ['', 'word', 'three', '', 'five']

I just want the output to be:

a = ['word', 'three', 'five']
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
kendi
  • 3
  • 4
  • 1
    Possible duplicate of [Remove empty strings from a list of strings](https://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings) – Sheldore May 28 '19 at 15:38
  • `list.remove` only removes the first occurrence of an item if it exists and can raise an error if it's not there. Instead you can use the fact that `bool('')` is False. This means you can do something like `a = ['', 'word', 'three', '', 'five']` then `[ele for ele in a if ele]` where the non-empty strings will evaluate to True and the empty string '' will evaluate to False and be excluded – Chrispresso May 28 '19 at 15:40

4 Answers4

2

You can apply list comprehension to rewrite original list missing zero-length strings.

Code:

a = ['', 'word', 'three', '', 'five']
a = [item for item in a if item]

Output:

['word', 'three', 'five']
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • @Carcigenicate, there's one line of code which use built-in feature. It's hard to write detailed explanation. Added link to docs. – Olvin Roght May 28 '19 at 15:41
0

The shortest way using list generators:

[elem for elem in a if elem] (This will filter out Nones, 0 and Falses)


The good short way:

[elem for elem in a if elem != '']


Using filter built-in function with lambdas:

list(filter(lambda e: e != '', a))


The generic way - construct the list of indices to remove and then remove them from the original list:

indices_to_remove = []
for i, elem in enumerate(a):
    if elem == '':  # Allows to use a complicated logic
        indices_to_remove.append(i)
for i in sorted(indices_to_remove, reverse=True):
    del a[i]
vurmux
  • 9,420
  • 3
  • 25
  • 45
  • You can just pass `None` to `filter()`. Quote from [docs](https://docs.python.org/3/library/functions.html#filter): `Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if item) if function is None.` – Olvin Roght May 28 '19 at 15:44
  • Than there will be a problem similar to the first - this will filter out Nones, 0 and Falses – vurmux May 28 '19 at 15:45
  • But in question mentioned that `I have this list that contains strings`. – Olvin Roght May 28 '19 at 15:46
0

You can just do like below. filter function does that by default.

a = ['', 'word', 'three', '', 'five']

print(list(filter(None, a)))

Output:

['word', 'three', 'five']
Praveenkumar
  • 2,056
  • 1
  • 9
  • 18
0

You can compare the length of the string and append to the list whose length is more than 1

You can do it by:

a = ['', 'word', 'three', '', 'five']
b = []

for x in a:
    if len(x) > 1:
        b.append(a)

You can also do it by list comprehensions

a = ['', 'word', 'three', '', 'five']
b = [i for i in a if len(i) > 1]
Santosh Karki
  • 77
  • 1
  • 9