Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list.reverse does not return list?

The return object is named None for list.reverse(). So this code fails when I call solution(k). Is there any way I can get around making a temporary? Or how should I do it?

fCamel = 'F' bCamel = 'B' gap = ' '  k = ['F', ' ', 'B', 'F']  def solution(formation):     return ((formation.index(bCamel) > (len(formation) - 1 - (formation.reverse()).index(fCamel)))) 

p.s. This is my first code in python. I thought it was functional.

like image 563
nakiya Avatar asked Nov 25 '10 20:11

nakiya


People also ask

Does reversed return a list?

The reverse() method doesn't return any value. It updates the existing list.

Does list Reverse () method return a value?

The list. reverse() method returns None because it reverses the list in place. It doesn't return a new reversed list.

How do I show a list in reverse?

Method 1: Reversing a list using the reversed() and reverse() built-in function. Using the reversed() method and reverse() method, we can reverse the contents of the list object in place i.e., we don't need to create a new list instead we just copy the existing elements to the original list in reverse order.

What does reverse return in Python?

Python reversed() method returns an iterator that accesses the given sequence in the reverse order.


1 Answers

You can use slicing to return the reversed list:

l[::-1] 
like image 154
Leon Avatar answered Oct 01 '22 08:10

Leon