+ 3

why output is not "hello"?

a = ['w','a','z'] if a.reverse()== a[::-1]: print("hello") else: print(False)

1st Sep 2018, 12:04 PM
Meet Rajpopat
Meet Rajpopat - avatar
2 Respostas
+ 9
The `reverse` method does not return the reversed list. What you are looking for is `list(reversed(a)) == a[::-1]`. For more info on a similar procedure: https://www.sololearn.com/Discuss/1468559/why-can-t-you-directly-use-the-insert-method-on-a-list-see-description
1st Sep 2018, 12:25 PM
Eduardo Petry
Eduardo Petry - avatar
+ 6
The reverse() method doesn't create a reversed copy of the list - it reverses the actual list itself 'in place'. Try this: a = ['w','a','z'] b = a[::-1] a.reverse() if a == b: # or you could say if a == ['z', 'a', 'w']: print("hello") else: print(False) See https://dbader.org/blog/JUMP_LINK__&&__python__&&__JUMP_LINK-reverse-list
1st Sep 2018, 12:27 PM
David Ashton
David Ashton - avatar