2
def manualReverse(list):
    return list[::-1]

    def reverse(list):
        return list(reversed(list))   

list = [2,3,5,7,9]

print manualReverse(list)
print reverse(list)

I just started learning Python. Can anyone help me with the below questions?

1.How come list[::-1] returns the reversed list?

2.Why does the second function throw me NameError: name 'reverse' is not defined?

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
theJava
  • 14,620
  • 45
  • 131
  • 172
  • 2
    See [The Python Slice Notation](http://stackoverflow.com/q/509211) for the slice notation. – Martijn Pieters Jul 11 '13 at 11:21
  • 1
    You indented the function too far; it is not defined at the global level, so `reverse` is a **local** name inside `manualReverse`. Un-indent it. – Martijn Pieters Jul 11 '13 at 11:22
  • possible duplicate of [The Python Slice Notation](http://stackoverflow.com/questions/509211/the-python-slice-notation) – Martijn Pieters Jul 11 '13 at 11:22
  • Dedent your `def reverse` so it lines up with `def ManualReverse` - otherwise, you're defining a function *inside* a function.... indentation is important in Python – Jon Clements Jul 11 '13 at 11:22
  • 2
    Don't name your variable `list`. That will override the builtin name `list`, and indeed will cause an error in your code above once you fix the indentation. – interjay Jul 11 '13 at 11:23

3 Answers3

13

[::-1] is equivalent to [::1], but instead of going left to right, the negative makes it go right to left. With a negative step of one, this simply returns all the elements in the opposite order. The whole syntax is called the Python Slice Notation.

The reason why 'reverse' is not defined is because you did not globally define it. It is a local name in the manualReverse function. You can un-indent the function so it is a global function.

def manualReverse(list):
    return list[::-1]

def reverse(list):
    return list(reversed(list))   

By the way, it's never a good idea to name lists list. It will override the built-in type, including the function too, which you depend on ( list(reversed(list)) )

Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • `manualReverse` will also successfully reverse a tuple and return a tuple, but `reverse` as shown will return the reverse of a tuple as a list. Better `reverse` code would be (assuming the `list` arg is renamed to `seq`): `return type(seq)(reversed(seq))`. – PaulMcG Jul 11 '13 at 12:19
2

list[::-1] utilizes a slice notation and returns all the elements but in reversed order. Explain Python's slice notation Here is a detailed explanation with examples - it will answer this and more similar questions.

Indentation of def reverse(list) makes it visible only inside manualReverse(list). If You unindent it will become visible globally.

Community
  • 1
  • 1
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • The question is "How come list[::-1] returns me the reversed list". Your answer basically repeats it in a slightly different wording. – interjay Jul 11 '13 at 11:30
  • @interjay I am not sure what is expected as the answer for "how come". The direct answer would be "because it is defined like that" – Grzegorz Piwowarek Jul 11 '13 at 11:33
  • `[::-1]` is not magic syntax to reverse a list. It is an instance of slice notation which is a more general concept, and a real answer would explain that and/or refer to the documentation. – interjay Jul 11 '13 at 11:33
0

Simply use the builtin function reversed

>>> reversed(my_list)

See http://docs.python.org/2/library/functions.html?highlight=reversed#reversed

P. M.
  • 79
  • 5
  • 1
    My opinion is to use builtin functions every time it's possible. It's more optimized, cleaner, more readable. Using the [::-1] syntax is not the right way to reverse a list. In the question, apart from indentation error, the reversed function is not used correctly – P. M. Jul 11 '13 at 16:14
  • You are entitled to your opinion. But this post does not answer the question at all. The `reversed` function is used correctly in the question, except for the error in using `list` as both a variable name and a builtin. – interjay Jul 11 '13 at 16:27
  • @P.M. You are right but guy is obviously trying to learn something here. – Grzegorz Piwowarek Jul 11 '13 at 16:55