0

I'm struggling with some NESTED LISTS. Briefly, inside the list of lists I have some lists containing several value

biglist = [[['strings', '632'], ['otherstrings', 'hey']],[['blabla', '924'], ['histring', 'hello']]]

from this nested list, I'd like to remove the sublist in which 'hello' string appears. I tried this:

for sub_line in big_list:
    if 'hello' in sub_line:
        big_list.remove(sub_line)

Now, if I print the new big_list outside the loop, I get the old list since I didn't assign the updated list to a new list. But if I assign to a new list like:

for sub_line in big_list:
    if 'hello' in sub_line:
        updated_list = big_list.remove(sub_line)
print(updated_list)

I get an AttributeError: 'NoneType' object has no attribute 'remove'.

So what's the problem with this? I CANNOT use list indexing because my real list is huge and the target value is not always in the same place. I've already check other questions but nothing is working. Thank you all!

Marco
  • 77
  • 1
  • 10
  • 2
    Related/possible dupe: [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – pault Dec 13 '19 at 13:33
  • @pault , Actually my list is way more complex. I have sometihing like this: `big_list = [[['string1', 'string2'],['string4','string5']], [['bla','bla'],['bla','bla']]]`. I'd use list comprehension to remove specific list from the inner lists. – Marco Dec 13 '19 at 14:09
  • If your input is more complex, then you should specify that in the question. Still, referring to [this answer](https://stackoverflow.com/a/1207461/10197418), in `somelist = [x for x in somelist if not determine(x)]`, `determine(x)` could be a function that analyses each element of `somelist`. – FObersteiner Dec 13 '19 at 14:27

3 Answers3

0

Following works for me. You need to remove sub_line (not line) form the list.

big_list = [['strings', '632', 'otherstrings', 'hey'],['blabla', '924', 'histring', 'hello']]

print(big_list)
for sub_line in big_list:
    if 'hello' in sub_line:
        big_list.remove(sub_line)
print(big_list)
Vijay
  • 778
  • 4
  • 9
  • Mhh, probably I copied wrongly my code. The "line" should be "sub_line", yes. :) Edited – Marco Dec 13 '19 at 13:30
0
for sublist in biglist:
      if 'hello' in sublist:
            updated_list=biglist.remove(sublist)
print(updated_list)

The output of the above code is None because remove() does not return any value i.e, it returns None. So you should not assign return value of remove() in a list. I think that might cause some problems whenever you will use updated_list.

saurabh sisodia
  • 312
  • 1
  • 8
0

if you constantly have two levels of nesting (not what I would label DEEP), you could combine this answer from the dupe marking by @pault with list flattening:

biglist = [[['strings', '632'], ['otherstrings', 'hey']],[['blabla', '924'], ['histring', 'hello']]]
token = 'hello'
smalllist = [x for x in biglist if not token in [j for i in x for j in i]]

# smalllist
# Out[17]: [[['strings', '632'], ['otherstrings', 'hey']]]
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • thank you. I've just started learning/working with python and maybe this nested list is not so deep but the huge amount of data I have inside, makes it looks deep for me. So I succeeded in my aim. I accepted your answer as correct because it provides me further advices. On the other hand, I went through all my script and I correct the nested list from the beginning using only partially your reply. Anyway thanks,I'll better focus on list comprehension! – Marco Dec 13 '19 at 15:53
  • 1
    @Marco: glad I could help! keep in mind though that this code will only work correctly if you have regular nesting, i.e. that each element of the "main list" is always a list of lists. If not, you will have to include checks and so on - in this case, using an ordinary `for` loop will make for more readable code compared to putting it all in a list comprehension. – FObersteiner Dec 13 '19 at 16:37