Let’s say I have the following:
['I', 'am', 'left', '', 'handed']
How do I remove index 3 from the list?
Let’s say I have the following:
['I', 'am', 'left', '', 'handed']
How do I remove index 3 from the list?
There’s a built-in list
method for exactly that…
https://docs.python.org/3/tutorial/datastructures.html?highlight=pop
The docs are worth reading.
If you are unsure of the position of the offending element in a given list, or if the number of occurrences may vary, you could use something like this:
while '' in myList:
myList.remove('')
There are many options, and as @thepitycoder pointed out, the docs are most definitely worth reading.
Aside
Consider that when we, the_string.split(separator_string)
, we are expected to reconstruct that exact string with separator_string.join(the_split_list)
.
If we remove the empty strings from the split version, we cannot replicate the original string from that result.
Meant to come back with an example, but got called away.
>>> m = 'mississippi'.split('s')
>>> m
['mi', '', 'i', '', 'ippi']
>>> n = filter(lambda x: len(x) > 0, m)
>>> n
['mi', 'i', 'ippi']
>>> 's'.join(n)
'misisippi'
>>> 's'.join(m)
'mississippi'
>>>