0
>>> n
['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', '']
>>> for item in n:
...    if item=='':
...      n.remove(item)
... 
>>> n
['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', 'AGC', 'S', 'GGC', 'G', 'UGA', 'Stop', 'CGA', 'R', 'AGA', 'R', 'GGA', 'G', 'UGG', 'W', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', '']

How to explain that iterative "remove" operation can't remove all the '' elements in the list?

2
  • 1
    Because a single remove removes only 1 item. I think you need to rephrase your question because its not very clear Commented Feb 4, 2015 at 20:07
  • 3
    You're altering a list as you're iterating over it. That is not a very good idea. Commented Feb 4, 2015 at 20:09

5 Answers 5

1

You can also use a list comprehension:

>>> n = ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', '']    

>>> n = [x for x in n if x != '']   # or more simply [x for x in n if x]
>>> print n

['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', 'AGC', 'S', 'GGC', 'G', 'UGA', 'Stop', 'CGA', 'R', 'AGA', 'R', 'GGA', 'G', 'UGG', 'W', 'CGG', 'R', 'AGG', 'R', 'GGG', 'G']
Sign up to request clarification or add additional context in comments.

Comments

1

To expand a bit on why you shouldn't change a list this way as you're iterating over it, consider the following simplification of what you're doing:

>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for item in a:
...     print item
...     a.remove(item)
... 
0
2
4
6
8
>>> a
[1, 3, 5, 7, 9]

As this illustrates, by changing the list, you're also changing which elements you act on in successive iterations. That's almost never a good idea and appears not to be what you're trying to accomplish.

Instead, how about a list comprehension:

[item for item in mylist if item != '']

1 Comment

I see the point. If I delete elements during iteration, every time one item will be skipped.Thanks!
0

Another option is

while '' in n:
    n.remove('')

Comments

0

Instead of trying to remove the blanks, try instead to make a new array which contains only the non-blank items

a = ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', '']

b = []
for x in a:
    if len(x)>0:
        b.append(x)

Comments

0

As dylrei mentioned, list comprehension is a pythonic solution, but is has a drawback with (very) huge list since it copies the list. If you have a huge list and the item you want to remove is only a few times in the list, then I suggest the following (less pythonic) solution:

>>> l=[1,2,3,4,5,6,1,2,3,1,2,1,1]
>>> while True:
...     try:
...         l.remove(1)
...     except ValueError:
...         break
... 
>>> l
[2, 3, 4, 5, 6, 2, 3, 2]

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.