[Tutor] modification to a list
Subhash Nunna
subhashnunna at hotmail.com
Wed Apr 22 11:59:46 EDT 2020
Hi Shubham,
Please try this:
def skip_elements(elements):
new_list = [ ]
a = len(elements)+1
if a==1:
return new_list
else:
for i in range(0, a):
if i%2==0:
new_list.append(elements[i])
return new_list
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"]))
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi','Peach']))
print(skip_elements([ ]))
[cid:image002.png at 01D618ED.234A0E60]
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