Skip to content
Advertisement

Modifying the contents of a list Python

Here’s the coursera Google question prompt that I’m having issues with:

The skip_elements function returns a list containing every other element from an input list, starting with the first element. Complete this function to do that, using the for loop to iterate through the input list

Original Code:

def skip_elements(elements):
  # Initialize variables
  new_list = []
  i = 0

  # Iterate through the list
  for ___
      # Does this element belong in the resulting list?
      if ___
          # Add this element to the resulting list
          ___
      # Increment i
      ___

  return ___

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 []


What I have:

def skip_elements(elements):
  # Initialize variables
  new_list = []
  i = 0

  # Iterate through the list
  for i in elements:
      # Does this element belong in the resulting list?
      if i %% 2 == 0:
          # Add this element to the resulting list
          new_list.insert(i)
      # Increment i
      i += 1

  return new_list

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 []

Advertisement

Answer

With minimal change to your code and no enumerate:

def skip_elements(elements):
  # Initialize variables
  new_list = []
  i = 0

  # Iterate through the list by value and not index
  for elem in elements:
      # Does this element belong in the resulting list? Check on index
      if i %% 2 == 0:
          # Add this element to the resulting list
          new_list.append(elem)
      # Increment index
      i += 1

  return new_list

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 []

The classical way:

def skip_elements(elements):
  # Initialize variables
  new_list = []

  # Iterate through the list by index
  for i in range(len(elements)):
      # Does this element belong in the resulting list?
      if i %% 2 == 0:
          # Add this element to the resulting list (value at index)
          new_list.append(elements[i])

  return new_list

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 []

Output:

['a', 'c', 'e', 'g']
['Orange', 'Strawberry', 'Peach']
[]

And because we like it: the one-liner

def skip_elements(elements):
  return [elements[i] for i in range(len(elements)) if i %% 2 == 0 ]

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement