Hi all. I’m a beginner in Python and trying to teach myself, so this might be a silly question.
I am confused about how to use class methods, particularly list methods (but this could apply to handling any class), and how they are called within a print function as opposed to outside of one.
My question is basically, why does print(family.sort()) print None where print(family.pop()) prints the last name on the list? I would have expected the print function calling sort() to print a sorted list, like the pop() function prints the last name in the list while removing it.
family.sort() is sorting the list in place. It doesn’t return the sorted list afterwards. If you used sorted(family) you’d get a new copy of the list in sorted order–the original list would be unchanged.
Meanwhile, pop is returning the thing it popped off the end of the list, so print(family.pop()) shows you the value.
It’s not crazy that list.sort() would return an additional reference to the same list, but (like the docs say) this can lead to later issues if people forget they have two references to the same list.
Side note: there’s no difference between being inside or outside of a print function (or any other function for that matter). They are called the same and act the same.
The main problem you are having is that list.sort() is not for returning the sorted list, but for sorting the list in place. It just returns None. To use it you have to do
family.sort()
print(family)
The function sorted would give you back the sorted list. With it you could do
In trying to understand what is meant by “in place”, am I correct in thinking about it as pop() returns a value that isn’t the same as what went into the function (the input), where sort() takes the list as input, modifies it and returns None because it’s the same list, just modified?
Okay, what seemed to help me understand better is actually playing around a bit more with the sort() function and some other ones, like remove(). It is actually unnecessary to return the modified list because the variable, family, refers to the modified list and you and just keep using that variable to refer to the list. If you want a new sorted list, use sorted().
After that I was able to actually sort the list and print it all on the same line like this: print(f"{str(family.sort()).removeprefix('None')}{family}")
Thank you to everyone that helped train my brain for Python.
Well, that depends on the problem you’re trying to solve. If the problem you’re trying to solve is to sort and print a list all on the same line and don’t intend on others to read you code, or you have the proper comments, then that solves the problem.
James showed a more efficient way by using a semi-colon. I wasn’t aware of that, but I’m assuming that is not the preferred method.
Yes, James’s is very slightly more efficient. Mine just has the “advantage” of being more like yours and being one expression, so it can be used in more places. Generally preferred method is to use two lines.
You can also create a function so that the sorting step is done for you. This will allow you to make the sorting function call more cleanly as demonstrated below.
def sorted_list(some_list):
some_list.sort()
return some_list
print(f'This is my sorted list: {sorted_list(family)}')
This way, you don’t have to drag along the sorting step every time when printing the sorted list.