Can someone explain this .sort() method behaviour please? /u/NewtLong6399 Python Education

Hi,

I was playing about with lists and this surprised me. Can anyone explain please?

Where I use the “.sort()” method in the assignment to the “e” variable below, I expected a sorted list to be assigned to “e”, instead “e” == None. I can use the built-in “sorted” function and it works. If I assign to a variable first (“c”) and then apply the .sort(), then it works. Why isn’t .sort() working when assigning to “e”?

>>> a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

>>> c = list(set([b for b in a]))
>>> print(c)
[1, 2, 3, 34, 5, 8, 13, 21, 55, 89]

>>> d = c.sort() # This works as expected
>>> print(d)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

>>> e = list(set([b for b in a])).sort() # Expected to see the same output as "d" above and "f" below
>>> print(e)
None

>>> f = sorted(list(set([b for b in a])))
>>> print(f)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

submitted by /u/NewtLong6399
[link] [comments]

​r/learnpython Hi, I was playing about with lists and this surprised me. Can anyone explain please? Where I use the “.sort()” method in the assignment to the “e” variable below, I expected a sorted list to be assigned to “e”, instead “e” == None. I can use the built-in “sorted” function and it works. If I assign to a variable first (“c”) and then apply the .sort(), then it works. Why isn’t .sort() working when assigning to “e”? >>> a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] >>> c = list(set([b for b in a])) >>> print(c) [1, 2, 3, 34, 5, 8, 13, 21, 55, 89] >>> d = c.sort() # This works as expected >>> print(d) [1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> e = list(set([b for b in a])).sort() # Expected to see the same output as “d” above and “f” below >>> print(e) None >>> f = sorted(list(set([b for b in a]))) >>> print(f) [1, 2, 3, 5, 8, 13, 21, 34, 55, 89] submitted by /u/NewtLong6399 [link] [comments] 

Hi,

I was playing about with lists and this surprised me. Can anyone explain please?

Where I use the “.sort()” method in the assignment to the “e” variable below, I expected a sorted list to be assigned to “e”, instead “e” == None. I can use the built-in “sorted” function and it works. If I assign to a variable first (“c”) and then apply the .sort(), then it works. Why isn’t .sort() working when assigning to “e”?

>>> a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

>>> c = list(set([b for b in a]))
>>> print(c)
[1, 2, 3, 34, 5, 8, 13, 21, 55, 89]

>>> d = c.sort() # This works as expected
>>> print(d)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

>>> e = list(set([b for b in a])).sort() # Expected to see the same output as "d" above and "f" below
>>> print(e)
None

>>> f = sorted(list(set([b for b in a])))
>>> print(f)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

submitted by /u/NewtLong6399
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *