0

I have array

a=['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '151 ihi Chun', '151 ihi Chun', '149 st Hg', '149 st Hg', '125 Tatane', '125 Tatane', '174 Sunnygat', '174 Sunnygat', '174 Sunnygat', '126 Nank', '126 Nank', '162 Rass', '162 Rass']

I want to remove all '' objects, but cant.

a.remove('')

or while a.index(''): a.remove('')

Are don't help..

1
  • 4
    For the record, in python that is called a list, not an array. :-) Commented Mar 13, 2013 at 10:12

3 Answers 3

6

Use a filter() call with None as the filter (tests for truth, so non-emptyness):

a = filter(None, a)

or a list comprehension:

a = [e for e in a if e]

If you need to explicitly allow other 'false' values and only want to filter out empty strings, use:

a = [e for e in a if e != '']
Sign up to request clarification or add additional context in comments.

Comments

0

If those items are actually '', in other words, empty strings, then you can use the following:

a = [x for x in a if x]

Since an empty string evaluates to false when used in a truth testing statement.

Comments

-1

try

for i in a:
   a.remove('')
   a.remove('')

i am also not sure why in first time it's not removing all but in second time sure it removes all the blank

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.