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?

5
  • 2
    See The Python Slice Notation for the slice notation. Commented Jul 11, 2013 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. Commented Jul 11, 2013 at 11:22
  • possible duplicate of The Python Slice Notation Commented Jul 11, 2013 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 Commented Jul 11, 2013 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. Commented Jul 11, 2013 at 11:23

3 Answers 3

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)) )

Sign up to request clarification or add additional context in comments.

1 Comment

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)).
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.

3 Comments

The question is "How come list[::-1] returns me the reversed list". Your answer basically repeats it in a slightly different wording.
@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"
[::-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.
0

Simply use the builtin function reversed

>>> reversed(my_list)

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

3 Comments

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
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.
@P.M. You are right but guy is obviously trying to learn something here.

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.