[issue43150] Last empty string not removed in a list

2021-02-06 Thread Kumar Makala
Kumar Makala added the comment: Thanks Steven! -- ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://ma

[issue43150] Last empty string not removed in a list

2021-02-06 Thread Steven D'Aprano
Steven D'Aprano added the comment: The problem here is that you are shortening the list as you walk along it, which means you skip items. You expected to visit: "Emma", "Jon", "", "Kelly","Eric", "", "", "KXN", "" in that order, but after visiting Emma, Jon and the first empty string, yo

[issue43150] Last empty string not removed in a list

2021-02-06 Thread Steven D'Aprano
Steven D'Aprano added the comment: This is not a language bug, it is a bug in your code: you are modifying the list as you iterate over it. There are lots of ways to do that task correctly, perhaps the easiest is with a filter: str_list = list(filter(bool, str_list)) or a list comprehe

[issue43150] Last empty string not removed in a list

2021-02-06 Thread Kumar Makala
New submission from Kumar Makala : # Last empty string not removed from the list str_list = ["Emma", "Jon", "", "Kelly","Eric", "", "","KXN",""] for item in str_list: if len(item) == 0: str_list.remove(item) print(len(str_list[-1])) #Output ['Emma', 'Jon', 'Kelly', 'Eric', 'KXN',