2

I faced an issue with my code where the loop stops running once it removes the list from the list of list.

data=[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]]

for word_set in data:
    if word_set[-1]!="hello":
        data.remove(word_set)
print(data)

My desired output is

[['why', 'why', 'hello'], ['why', 'cry', 'hello']]

but the output is

[['why', 'why', 'hello'], ['why', 'hi', 'sllo'], ['why', 'cry', 'hello']]

How do I make the loop go on till the end of the list?

5 Answers 5

4

That's because, when you remove the second item (whose index is 1), the items after it move forward. In the next iteration, the index is 2. It should have been pointing to ["why","hi","solo"]. But since the items moved forward, it points to ["why","cry","hello"]. That's why you get the wrong result.

It's not recommended to remove list items while iterating over the list.

You can either create a new list (which is mentioned in the first answer) or use the filter function.

def filter_func(item):
    if item[-1] != "hello":
        return False
    return True

new_list = filter(filter_func, old_list)
Sign up to request clarification or add additional context in comments.

Comments

2

Remember

data = [["list", "in","a list"],["list", "in","a list"],["list", "in","a list"]]
#data[0] will return ["list", "in","a list"]
#data[0][0] will return "list"
#remember that lists starts with '0' -> data[0]

Comments

1
>>> data=[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]]
>>> y = []
>>> for subData in data:
        for dataItem in subData:
            if dataItem == "hello":
                y.append(subData)
>>> y
[['why', 'why', 'hello'], ['why', 'cry', 'hello']]

Comments

1
filter(lambda x : x[-1] =="hello",[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]])

OR

reduce(lambda x,y : x + [y] if y[-1]=="hello" else x ,[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]],[])

OR

[i for i in [["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]] if i[-1]=="hello"]

Comments

1
data=[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]]

for word_set in data[:]:
    if word_set[-1]!= "hello":
        data.remove(word_set)
print(data)

Don't iterate the origin data,but make a duplicate(data[:]). Because when remove items from list, the index of item will change.["why","why","bell"] in list index is 1. when it's removed from data. ["why","hi","sllo"] in data index will be 1. The next iteration index is 2, so ["why","hi","sllo"] is passed and checks ["why","cry","hello"].

1 Comment

Please add some comment as to why you think this code is the answer, as it stands, this answer is not very useful.

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.