[Tutor] modification to a list
Subhash Nunna
subhashnunna at hotmail.com
Sat Apr 25 00:03:32 EDT 2020
Hi Shubham,
Here is the code meeting you’re conditions:
def skip_elements(elements):
# Initialize variables
new_list = []
i = 0
# Iterate through the list
for i,element in enumerate(elements):
# Does this element belong in the resulting list?
if elements.index(element)%2==0:
# Add this element to the resulting list
new_list.append(element)
# 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 []
Regards,
Subhash Nunna
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
From: shubham sinha<mailto:upwkwd at gmail.com>
Sent: Wednesday, April 22, 2020 8:17 PM
To: tutor at python.org<mailto:tutor at python.org>
Subject: [Tutor] modification to a list
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']
[]
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list