You can’t remove items from a list while iterating over it. It’s much easier to build a new list based on the old one:
y = [s for s in x if len(s) == 2]
More Related Contents:
- Transpose/Unzip Function (inverse of zip)?
- Python List vs. Array – when to use?
- Getting the index of the returned max or min item using max()/min() on a list
- Why can tuples contain mutable items?
- How can I iterate through two lists in parallel?
- Find and replace string values in list
- Pairs from single list
- Splitting a list based on a delimiter word
- How to find most common elements of a list? [duplicate]
- How do you pick “x” number of unique numbers from a list in Python?
- What is the time complexity of popping elements from list in Python?
- How to convert list of key-value tuples into dictionary?
- Finding index of an item closest to the value in a list that’s not entirely sorted
- Python split string into multiple string [duplicate]
- How to convert a list to a list of tuples?
- How to input matrix (2D list) in Python?
- Combining Dictionaries Of Lists In Python
- Python passing list as argument
- Check if list items contains substrings from another list
- Python Bool and int comparison and indexing on list with boolean values
- Comprehension on a nested iterables?
- Python: Remove Duplicate Items from Nested list
- Changing one dict changes all dicts in a list of dicts [duplicate]
- How to sort multidimensional array by column?
- How to sort a 2D list? [closed]
- sum each value in a list of tuples
- How can I convert string values from a dictionary, into int/float datatypes?
- Python: Elegant and efficient ways to mask a list
- list comprehension with multiple conditions (python)
- How to convert only numbers in a mixed list into float