I'm working on a beginning python exercise with strings and methods. (I know that there are some extra print statements and comments.) The goal is to return every other item from the list. So for the first example the goal is to return - # Should be ['a', 'c', 'e', 'g']
I'm struggling, because I can not figure out why my code won't delete 'b' from the list. What am I missing? .
I've looked at the following specific StackOverflow questions. (Plus others)
- Need help on List of Strings
- How to remove an element from a list by index
- Remove empty strings from a list of strings
- Difference between del, remove and pop on lists
def skip_elements(elements):
L_elements = len(elements)
for x in elements:
print("This is L_elements: " , L_elements)
#print(x)
if (L_elements % 2 == 0):
elements.pop((L_elements-1))
print(elements)
if (L_elements == 2):
print("This is L_elements: " , L_elements, "Should equal 2")
L_elements = L_elements - 1
return elements
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']
print(skip_elements([])) # Should be []