0

I have this function to simulate reverse() in python:

My_list = [[1, 3, "s"], 2, 2, 3, 4, 2]
def listRev(list):
    list=list[::-1]
    print list
listRev(My_list)
print My_list

The function gives me correct reversed list but I expect when I print My_list in the last line, the code print reversed My_list not My_list itself. How can I solve my problem?

2
  • 3
    Usling list as a veriable name is a bad idea. Commented Dec 17, 2014 at 8:38
  • @Marcin , I am agree with you Commented Dec 17, 2014 at 8:46

2 Answers 2

3

If you want to simulate reverse, you must modify the list passed as a parameter to you function, not simply use a new list in your function.

When you write list=list[::-1] the local copy of the original parameter points to a new list. You should do instead :

def rev(l):
    l[:] = l[::-1]
    print (l)

The usage of l[:] asks Python to replace the content of the list, and not to make the variable point to a new list.

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

2 Comments

@StackOverHello if you want to do it algorithmicaly like we do in C language you can write: def my_list_reverse(L): length = len(L) for i in range(length/2): L[i], L[length-i-1] = L[length-i-1], L[i]
@StackOverHello your welcome! btw Good suggestion is posted by Serge Ballesta - Writing C kind of verbose code in Python would be a bad idea! - Python is a scripting language it would be better to use tools and tricks belongs to language. I just share this code with you as I have impression that you are new in Python
2

This can be solved by returning a list from the function

My_list = [[1, 3, "s"], 2, 2, 3, 4, 2]
def listRev(list):
    list=list[::-1]
    print list
    return list
My_list = listRev(My_list)
print My_list

This is because lists are not passed by reference in Python.

Post Comment Edit

If you want the function to be in place only for My_list even though it is very wrong and bad programming to do so, you can use this

My_list = [[1, 3, "s"], 2, 2, 3, 4, 2]

def listRev():
        global My_list
        My_list =  My_list[::-1]

listRev()
print My_list

6 Comments

Thank you. But I want the function itself returns me reversed My_list
Look if I delete this line: My_list = listRev(My_list) then result is My_list it self. That line is not a part of function.
@StackOverHello If you delete the line My_list = listRev(My_list) then you will not be calling that function. Thus my_list will be My_list itself and will not be reversed.
Yes. If I just call function like this: listRev(My_list) I just have My_list it self when I print My_list
@StackOverHello That will not be inplace as lists are not copied by reference. If you want to do that then declare My_list as global. and change it there itself.
|

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.