2

I have a list as

[['Name', 'Place', 'Batch'], ['11', '', 'BBI', '', '2015'], ['12', '', 'CCU', '', '', '', '', '2016'], ['13', '', '', 'BOM', '', '', '', '', '2017']]

I want to remove all the '' from the list.

The code I tried is :

>>> for line in list:
...     if line == '':
...         list.remove(line)
...
print(list)

The output that it shows is:

[['Name', 'Place', 'Batch'], ['11', '', 'BBI', '', '2015'], ['12', '', 'CCU', '', '', '', '', '2016'], ['13', '', '', 'BOM', '', '', '', '', '2017']]

Can someone suggest what's wrong with this ?

5
  • Where do the nested lists come from? It would be better to create them without the '' in the first place. Commented May 5, 2017 at 13:15
  • @mkrieger1 I have a raw file which is not properly delimited. I converted to list and hence the output. Wanted to remove those unwanted values. Commented May 5, 2017 at 13:22
  • 1
    What I meant was: If you showed us how you created the lists, we could probably tell you how you could avoid the '' entries, and you wouldn't need to remove them again. Commented May 5, 2017 at 13:27
  • (might be related: meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Commented May 5, 2017 at 13:32
  • @mkrieger1 That would be even better. I would make a sample file and edit the post with the code i executed. Commented May 6, 2017 at 4:55

2 Answers 2

4

This is what you want:

a = [['Name', 'Place', 'Batch'], ['11', '', 'BBI', '', '2015'], ['12', '', 'CCU', '', '', '', '', '2016'], ['13', '', '', 'BOM', '', '', '', '', '2017']]
b = [[x for x in y if x != ''] for y in a]  # kudos @Moses
print(b)  # prints: [['Name', 'Place', 'Batch'], ['11', 'BBI', '2015'], ['12', 'CCU', '2016'], ['13', 'BOM', '2017']]

Your solution does not work because line becomes the entire sublist in every step and not the elements of the sublist.

Now it seems that you are trying to modify the original list in-place (without creating any additional variables). This is a noble cause but looping through a list that you are modifying as you are looping is not advisable and will probably lead to your code raising an error (IndexError or otherwise).

Lastly, do not use the name list since Python is using it internally and you are overwriting its intended usage.

Sign up to request clarification or add additional context in comments.

2 Comments

An explicit test might be better, since this filters out all falsy values
@MosesKoledoye True. Thanks!
3

You're running your test on the sublists not on the items they contain. And you'll need a nested for to do what you want. However removing items from a list with list.remove while iterating usually leads to unpredictable results.

You can however, use a list comprehension to filter out the empty strings:

r = [[i for i in x if i != ''] for x in lst]
#                       ^^ filter condition

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.