2

Given the following list:

a = ['aux iyr','bac oxr','lmn xpn']

c = []
for i in a:
    x = i.split(" ")
    b= x[1][::-1] --- Got stuck after this line

Can anyone help me how to join it to the actual list and bring the expected output

output = ['aux ryi','bac rxo','lmn npx']

1
  • Could there be more than two tokens per string and if so, what would happen after the second? Commented Dec 22, 2020 at 14:39

2 Answers 2

2

I believe you need two lines of codes, first splitting the values:

b = [x.split() for x in a]

Which returns:

[['aux', 'iyr'], ['bac', 'oxr'], ['lmn', 'xpn']]

And then reverting the order:

output = [x[0] +' '+ x[1][::-1] for x in b]

Which returns:

['aux ryi', 'bac rxo', 'lmn npx']
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the following simple comprehension:

[" ".join((x, y[::-1])) for x, y in map(str.split, a)]
# ['aux ryi', 'bac rxo', 'lmn npx']

Comments

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.