Modifies the list / returning a new list


in lesson “Python Lists and Dictionaries”, part " 9 more with ‘for’ " in the partition “Learn” it said this (" 2. Then, we sort animals into alphabetical order. Note that .sort() modifies the list rather than returning a new list.")

my question is which is the difference between the list changes and the creation of a new list, which program is made when I type the code "> animals.sort() "

thank you very much :slight_smile:


animals.sort() modifies the original list, where as sorted(animals) would create a new list.

so after .sort() we have a single list, after sorted we have two lists (sorted and unsorted)



and I have another question - what is meaning of “returning” ? sometimes it said ‘this do not return anything’


hasn’t the return keyword been featured yet? Its literally that, the function is handing you something back:

def example():
   return "hello world"

# store returned result in x variable
x = example()
print x

so sorted() also uses return to hand you back the sorted list


ok. thanks a lot :slight_smile:

have a nice day :vulcan_salute:


@stetim94 I have one more question pls.

In ‘for loop’ for dictionary it said : “Note that dictionaries are unordered, meaning that any time you loop through a dictionary, you will go through every key, but you are not guaranteed to get them in any particular order.” why python dont print keys in particular order? in task ‘a day in supermarket’ in ‘2. this is key’ it print out random keys. why is that so?


you answered your own question:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.