0

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)

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 []
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    You can keep every even index with a list comprehension. `[x for x in list if list.index(x) % 2 == 0]` – alec Feb 19 '20 at 04:47
  • 2
    @alec, please do not use `list` as a name. – Stephen Rauch Feb 19 '20 at 04:49
  • 1
    Sorry, I thought the statement would be clearer this way instead of using another variable name. As Stephen said, "list" is the name of a builtin function and shouldn't be used as a variable name. – alec Feb 19 '20 at 04:52

1 Answers1

2

If you look at the debug output from your program you'll see that your loop doesn't get L_elements all the way down to 1 as it should. This is because you are shortening elements in the loop and so the loop (for x in elements) doesn't execute as often as you expect it to. If you change your for x in elements to for i in range(L_elements) it will work as expected.

It's much simpler though to write this as a list comprehension:

def skip_elements(elements):
    return [e for i, e in enumerate(elements) if i % 2 == 0]
Nick
  • 138,499
  • 22
  • 57
  • 95
  • I get most of that- specifically that I reducing my loop an L_elements isn't getting to one. That's the key to following up. But what's "list comprehension" specifically in this context. I know that I can Google it, but that's a rabbit hole of pages. (And I will anyway.) I'm actually try to stay mostly within what I know. That comment may be simpler to you, but is almost physics on steroids to me. Thank you. – Lynn L Crawford Feb 19 '20 at 04:59
  • 1
    @LynnLCrawford Best to read the manual; it explains how a list (or dictionary) comprehension is basically shorthand for a for loop. https://docs.python.org/3/tutorial/datastructures.html – Nick Feb 19 '20 at 05:01
  • That's an excellent resource. I might just print it for awhile! Or at least leave it up in a window. – Lynn L Crawford Feb 19 '20 at 05:04