0

I want to reverse every other words in a list. The list that I have is -

['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']

How do I reverse every other word so the output should be.

['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK', 'Avacado', 'nomeL', 'Lime', 'yenoM']

The first letters don't have to be big I just had it like that. Much appreciated

3
  • 3
    Is this a homework problem? If so, we should only give hints rather than a solution. The hints are 1) use [::-1] to reverse an individual string and 2) use range(start, stop, step) to loop over specific indicies. Commented Nov 16, 2019 at 1:57
  • 1
    I agree, OP hasn't posted anything that they tried either. Commented Nov 16, 2019 at 2:24
  • @PyWalker2797 I actually ended up trying a lot of things, I ended up wasting a lot of time trying to fix the <list_reverseiterator object at 0x03D66328> error I got, I just wanted some help and ofcourse learn from the answer. I really liked Raymond Hettinger answer I learned a lot fro m that Commented Nov 16, 2019 at 10:52

7 Answers 7

4

Here's one simple way to do it:

>>> s = ['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']
>>> for i in range(1, len(s), 2):
        s[i] = s[i][::-1]

>>> s
['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK', 'Avacado', 'nomeL', 'Lime', 'yenoM']

The range(1, len(s), 2) starts at position 1 and counts by twos.

The [::-1] reverses a string.

Sign up to request clarification or add additional context in comments.

Comments

4

I feel many of the solutions posted are quite clever, but may feel slightly too comprehendible for a beginner. This solution is not simple and uses a single step to achieve the result.

>>> x = ['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']
>>> [v[::1-(i%2)*2] for i, v in enumerate(x)]
['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK', 'Avacado', 'nomeL', 'Lime', 'yenoM']

2 Comments

I appreciate the humour in this.
Glad I could bring a smile @PyWalker2797 =)
2

You can reverse a string word by writing word[::-1], so the task is just to apply this for every other element. This can be solved with a list comprehension, using enumerate and checking whether the index is odd or even.

>>> words = ['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon',
             'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']
>>> [ (word if i%2 == 0 else word[::-1]) for i, word in enumerate(words) ]
['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK',
 'Avacado', 'nomeL', 'Lime', 'yenoM']

Comments

1

I feel many of the solutions posted are quite clever, but may feel slightly impenetrable to a beginner. This solution is quite simple and uses simple steps to achieve the result.

words = ['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']

for index in range(0,len(words)):
    if index%2 != 0: #find every other 'index'
        words[index] = words[index][::-1] #reverse word at every other 'index'



1 Comment

NameError: name 'word' is not defined
0

In one line:

list1 = ['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']

[x[::-1] if i % 2 != 0 else x for i, x in enumerate(list1)]
['Bananas',
 'selppA',
 'Fruits',
 'etalocohC',
 'Watermelon',
 'iwiK',
 'Avacado',
 'nomeL',
 'Lime',
 'yenoM']

Comments

0

You could use map for iterating and enumerate to get the index. Check if the index is odd and then reverse using [::-1]

Python 2

>>> v=['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']
>>> map( lambda (i,x): x[::-1] if i % 2 == 1 else x , enumerate(v))
['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK', 'Avacado', 'nomeL', 'Lime', 'yenoM']

Python 3

list(map( lambda x: x[1][::-1] if x[0] % 2 == 1 else x[1] , enumerate(v)))

The above will give a new list. If you want same list to be modified, then

 >>> v=['Bananas', 'Apples', 'Fruits', 'Chocolate', 'Watermelon', 'Kiwi', 'Avacado', 'Lemon', 'Lime', 'Money']
 >>> for i,x in enumerate(v):
  ...  if i %2 == 1:
  ...    v[i]=v[i][::-1]
 >>> v
['Bananas', 'selppA', 'Fruits', 'etalocohC', 'Watermelon', 'iwiK', 'Avacado', 'nomeL', 'Lime', 'yenoM']

1 Comment

The question is tagged Python-3.x, so you should use list(map(...)) for the result to be a list.
-1

You can iterate through your list by 2 like so

for word in my_words[::2]:
    print(reversed(word)) 

This will step through your array but 2 and will print the word in reversed. You can also reverse a word by using word[::-1]

2 Comments

The problem is to reverse them in the list, not to print them. This also doesn't actually print the reversed words, because reversed doesn't return a string; it prints something like <reversed object at 0x0000000003144898>.
Sigh.. Fair enough. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.