Why is the enumerate function splitting each string in a list into individual characters?

  Kiến thức lập trình

I am trying to write a function that will take elements in a list and return every other element using the enumerate function.

The code I ran is as follows:

def skip_elements(elements):
even_elements = []
for index, element in enumerate(elements):
if index % 2 == 0:
even_elements += element

return even_elements

print(skip_elements([“a”, “b”, “c”, “d”, “e”, “f”, “g”]))
print(skip_elements([‘Orange’, ‘Pineapple’, ‘Strawberry’, ‘Kiwi’, ‘Peach’]))

The first list was successfully split, however, when the second list goes in, each string gets divided up into individual characters. What am I missing that’s causing this to happen?

New contributor

icemike89 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT