Skip to content
Advertisement

How to iterate odd indexes and add them back to original list

I want to add “…” after every odd index in my list (or after every second word), but I can’t quite figure out how to do it.

Advertisement

Answer

you can use something like that:

sentence = input().split()
ans = []
for i in range(len(sentence)):
  if i%2:
     ans.append(sentence[i]+'...')
  else:
     ans.append(sentence[i])
print(' '.join(ans))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement