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:
JavaScript
x
21
21
1
def skip_elements(elements):
2
# Initialize variables
3
new_list = []
4
i = 0
5
6
# Iterate through the list
7
for ___
8
# Does this element belong in the resulting list?
9
if ___
10
# Add this element to the resulting list
11
___
12
# Increment i
13
___
14
15
return ___
16
17
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
18
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be
19
['Orange', 'Strawberry', 'Peach']
20
print(skip_elements([])) # Should be []
21
What I have:
JavaScript
1
21
21
1
def skip_elements(elements):
2
# Initialize variables
3
new_list = []
4
i = 0
5
6
# Iterate through the list
7
for i in elements:
8
# Does this element belong in the resulting list?
9
if i % 2 == 0:
10
# Add this element to the resulting list
11
new_list.insert(i)
12
# Increment i
13
i += 1
14
15
return new_list
16
17
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
18
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be
19
['Orange', 'Strawberry', 'Peach']
20
print(skip_elements([])) # Should be []
21
Advertisement
Answer
With minimal change to your code and no enumerate:
JavaScript
1
21
21
1
def skip_elements(elements):
2
# Initialize variables
3
new_list = []
4
i = 0
5
6
# Iterate through the list by value and not index
7
for elem in elements:
8
# Does this element belong in the resulting list? Check on index
9
if i % 2 == 0:
10
# Add this element to the resulting list
11
new_list.append(elem)
12
# Increment index
13
i += 1
14
15
return new_list
16
17
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
18
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be
19
['Orange', 'Strawberry', 'Peach']
20
print(skip_elements([])) # Should be []
21
The classical way:
JavaScript
1
18
18
1
def skip_elements(elements):
2
# Initialize variables
3
new_list = []
4
5
# Iterate through the list by index
6
for i in range(len(elements)):
7
# Does this element belong in the resulting list?
8
if i % 2 == 0:
9
# Add this element to the resulting list (value at index)
10
new_list.append(elements[i])
11
12
return new_list
13
14
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
15
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be
16
['Orange', 'Strawberry', 'Peach']
17
print(skip_elements([])) # Should be []
18
Output:
JavaScript
1
4
1
['a', 'c', 'e', 'g']
2
['Orange', 'Strawberry', 'Peach']
3
[]
4
And because we like it: the one-liner
JavaScript
1
3
1
def skip_elements(elements):
2
return [elements[i] for i in range(len(elements)) if i % 2 == 0 ]
3