2

I have the following list:

 l = [["a", "done"], ["c", "not done"]]

If the second element of each sub list is "done" I want to remove that the sub list. So the output should be:

l = [["c", "not done"]]

Obviously the below doesn't work:

for i in range(len(l)):
    if l[i][1] == "done":
        l.pop(0)

5 Answers 5

6

Use list_comprehension. It just builts a new list by iterating over the sublists where the second element in each sublist won't contain the string done

>>> l = [["a", "done"], ["c", "not done"]]
>>> [subl for subl in l if subl[1] != 'done']
[['c', 'not done']]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

2
l = [["a", "done"], ["c", "not done"]]
print [i for i in l if i[1]!="done"]

or use filter

l = [["a", "done"], ["c", "not done"]]
print filter(lambda x:x[1]!="done",l)

Comments

0

Apply a filter for your criteria:

l = [["a", "done"], ["c", "not done"]]
l = filter(lambda x: len(x)>=2 and x[1]!='done', l)

Comments

0

status index is 1, you checked index 0

for i in range(len(l)):
       if(l[i][1] == "done"):
           l.pop(i)

2 Comments

its better to include a little explanation in your answer
I corrected the answer...But the for loop in range will not work because you will get an index out of range error
0

Use list comprehension:

l = [ item for item in l if item[-1] != 'done']

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.