0

I have a list of tuples:

[('a', 'a', 'a'), ('b', 'b', 'b), ('c',' \n     \n      \n ', 'c'), ('d', 'd', 'd'),('e', ' \n        \n      \n ', 'e'] 

I want to delete the tuples that contain whitespace. My code is:

for tuple in list:
    for string in tuple:
        if string.isspace()  == True:
            list.remove(tuple)

It only deletes the second of the tuples with whitespace. If I re-execute the code it deletes the other as well.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Do not use *list*, *tuple* and any other name of built-in function as name of variable, you're shadowing this function. – Olvin Roght Jan 04 '21 at 22:30
  • 2
    Don't change the contents of a list while you iterate over it, that causes issues – G. Anderson Jan 04 '21 at 22:30
  • If you really want to do `operations` while iterating the `list` - you can do this way: for item in lst[::-1]: ..... This will not affect the index for next item. – Daniel Hao Jan 04 '21 at 22:36
  • As I cant answer, may as well post as comment. `[t for t in inp if not any(n.isspace() for n in t)]`. Where `inp` is your input list. – Paul Rooney Jan 04 '21 at 22:37

0 Answers0