[Tutor] modification to a list

shubham sinha upwkwd at gmail.com
Wed Apr 22 10:44:35 EDT 2020


Hi,
Q. 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.
my code:
def skip_elements(elements):
    new_list = [ ]
    i = 0
    for element in elements:
        if "element" != "elements(i):
            new_list.append(element)

        i += 2
    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 [ ]

my output:

['a', 'b', 'c', 'd', 'e', 'f', 'g']
['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']
[]


output should be:

['a', 'c', 'e', 'g']

['Orange', 'Strawberry', 'Peach']

[]


More information about the Tutor mailing list