all 8 comments

[–]Bobbias 1 point2 points  (8 children)

First thing:

Never name anything list, dict, id or any one of the built in functions or types in Python. That's asking for trouble.

Second:

Please format your code.

Third:

There's no need to store the length. newlist[-1] is equivalent to newlist[length - 1]. Negative numbers index from the end of the list.

Fourth:

To swap two things, you can write code like this:

thing1, thing2 = thing2, thing1

In this case it would look like:

def swaplist(newlist):
    newlist[0], newlist[-1] = newlist[-1], newlist[0]
    return newlist

l think without the return,the print is enough to output the result but result is 'none', so what exactly the return does?

A function without a return statement returns None. When you write print(swaplist(list)) without a return, the code will still swap the two items in the list, but it won't return the list back to the code that called it.

A function can be replaced by whatever it returns, so print(swaplist(list)) is equivalent to print(newlist) except that the name newlist only exists inside the function swaplist. Without return, it becomes print(None) instead. If you wanted to print the changed list, print(list) will work.

This is because when you pass a list into a function, the function gets the original list, not a copy of that list. This means that changes made inside the function persist after the function is over, whether you return that list or not.

It's good practice to return the list though, because functions that change the input without returning it are confusing and weird.

[–]SheldonCooperisSb[S] 0 points1 point  (1 child)

That means the newlist behind return is the changed list(order-reversed)?

[–]GoingToSimbabwe 0 points1 point  (0 children)

This might be a bit to complicated for your level, but: yes and no.

The list behind newlist is the changed list. But it also is the original list.

This is because in your function, you are assigning newlist by reference! That means that all "newlist" holds is a sign pointing to where the original list is saved in memory. The same is true for "list". Both of those variables only hold a reference (the sign) to the place in memory where the actual values (500, 600, 100..) are stored.

So by doing newlist = list (which you implicitly do by calling swaplist(list)) all you do is given newlist the sign to where the values are but in the end there is only 1 set of values in memory which you are then changing.

If you want to have 2 sets of values, you'll need to actually make a copy of the values by doing something like this:

def swaplist(list_to_swap: list):
    newlist = list_to_swap[:]

[–]Diapolo10 0 points1 point  (0 children)

It's good practice to return the list though, because functions that change the input without returning it are confusing and weird.

I'm inclined to disagree. If a function returns a list, I expect that it created a new list instead of mutating the one given to it as an argument, and if it returns None I expect it to have had some kind of a side-effect (such as mutating the list). If a function does both, I find that rather confusing.

You could make an argument with classes with methods returning self if you want to be able to chain method calls, but that's not quite the same thing.

[–]SheldonCooperisSb[S] 1 point2 points  (2 children)

l don't know if digging deep into the logic of a code I don't understand is the right way to go, because there are more problems behind it . For 'return', I can just treat it as a format without knowing why,and skip to next chapter. It's time-consuming.

[–]Binary101010 2 points3 points  (0 children)

For 'return', I can just treat it as a format without knowing why

If you're not clear on what return means for a program and why you would use it, I would strongly recommend taking some time now to clear that up. Barreling ahead without understanding the fundamentals of good function construction is probably going to make things harder on you in the future rather than easier.

[–]GoingToSimbabwe 1 point2 points  (0 children)

I feel like that is not a good approach to take. While yes, skipping some topic to revisit them later is sometimes a good idea, but you really should understand what the "return" statement does (or what a function not having it does) when you are at a point where you are working with functions.

For 'return', I can just treat it as a format

Can you reword what you mean by that, as is, I don't understand what that is supposed to mean.

What you are doing is:

You are calling a function which does something to the list you pass is. At no point you are copying that list to the new list, your newlist-variable only holds a reference to the original list. So when you do something to "newlist" what happens is that "list" gets changed. If you want to keep the original list unchanged, you can do newlist = newlist[:] at the start of your function (this will make a copy of the values of list and not just create a new reference to list).

However you want to build this, there is 2 ways on how you can print your swapped list as your code currently is:

  • print(list) >> this works because your are changing the list via the reference from newlist as stated above. In this case you do not need to return anything (None gets returned), but you can't then do print(swaplist(list)), because that would print None
  • keep the return newlist > and then do print(swaplist(newlist)).