Using the .sort() method inside a print() function

Hello everyone,
I was just working through the topic: “WORKING WITH LISTS IN PYTHON: Sorting Lists I” and I cannot understand why this code keeps giving me a “None” result instead of printing out a sorted list of addresses to the console?

addresses = ["221 B Baker St.", "42 Wallaby Way", "12 Grimmauld Place", "742 Evergreen Terrace", "1600 Pennsylvania Ave", "10 Downing St."]
print(addresses.sort())

I have noticed that when I add an additional line of code, I get the desired result like so:

addresses = ["221 B Baker St.", "42 Wallaby Way", "12 Grimmauld Place", "742 Evergreen Terrace", "1600 Pennsylvania Ave", "10 Downing St."]
addresses.sort()
print(addresses)

I don’t understand why this is happening.

When you use the .sort() method, it modifies the list in place, not the original list and it will return None.

See:
https://docs.python.org/3/howto/sorting.html

and,
https://docs.python.org/3/library/stdtypes.html#list.sort

3 Likes