Modifying the contents of a list Python

Question:

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 []
Asked By: Ammon Fawson

||

Answers:

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 ]
Answered By: Synthase

I couldn’t do it with i= and finally wanted to post something on stackoverflow.

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

  # Iterate through the list
  for element in elements[::2]:
      new_list.append(element)

  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']
[]
Answered By: Anil

Statement
The skip_elements function returns a list containing every other element from an input list, starting with the first element

Logic being used here
The question is asking us to return every other which means the one at odd places such as with index values 0,2 etc.
So here we need to write the code in order to get the elements at the odd places

Code

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

    # Iterate through the list
    for x in elements:
        # Does this element belong in the resulting list?
        if i % 2 == 0:
            # Add this element to the resulting list
            new_list.append(x)
        # 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 []

Hope it helps 🙂

Answered By: Vaibhav Singh
def skip_elements(elements):
    # Initialize variables
    new_list = []
    i = 0                #think i as index for the list, thus it's zero

    # Iterate through the list
    for elements in elements:         #to iterate over the input list
        
        if i % 2 == 0:
            new_list.append(elements)
            # Adding this element to the resulting list
            
        i +=1         # Increment i for every iteration

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

Question focuses on list manipulation and wants us to explore contents of list by iteration via for loop and to better understand significance of ‘indexing’.
Otherwise, to so produce same logic following code word works too !

def skip_elements(elements):
    return elements[::2]     #slicing input list (start:stop:step) to skip next element

print(skip_elements(["a", "b", "c", "d", "e", "f", "g"]))
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']))
print(skip_elements([]))

Try it! ✌

Answered By: Krish
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.