listsortingsortedlist

Reverse order of a list


I am trying to write a python code to switch the order of the list (from A to Z:input, the output should print from Z to A). My code is not giving me the right output. I need help please

 winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer ``Weiss', 'Youyou Tu']
z_winners=sorted(winners, key=lambda x: x.split(" ")[-1])

my output is :

['Kazuo Ishiguro', 'Alice Munro', 'Alvin E. Roth', 'Youyou Tu', 'Rainer Weiss', 'Malala Yousafzai']

Instead of :

['Youyou Tu', 'Rainer Weiss', 'Malala Yousafzai', 'Kazuo Ishiguro', 'Alvin E. Roth', 'Alice Munro']

Solution

  • Try this

    winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer Weiss', 'Youyou Tu']
    
    z_winners=sorted(winners, reverse=True)
    
    print(z_winners)
    

    By the way, you have extra '`' in your list of strings.