classification
Title: List sorting
Type: behavior Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sivasundar Nagarajan, akarei, eric.smith, terry.reedy
Priority: normal Keywords:

Created on 2020-03-27 02:26 by Sivasundar Nagarajan, last changed 2020-03-27 06:01 by terry.reedy. This issue is now closed.

Messages (3)
msg365129 - (view) Author: Sivasundar Nagarajan (Sivasundar Nagarajan) Date: 2020-03-27 02:26
Good day. Hope you are safe and wish the same with the present situation.
Need you help please in understanding the below function of Python. 
 
 
Steps - 
1. tried assigning the below values in the list and named it as a
2.if I print, it prints in the same sequence.
3.Tried assigning it to b by the command a.sort()
4.Tried printing b and it gave null.
5.But printed a now, and the values were sorted.
 
 
Please help me understand if we have any logic with in a list to sort the values after an iteration.
Please apologize if I had missed some basics and uncovered it. 
 
>>> a = [1,4,3,2,4,5,3,2]
>>> a
[1, 4, 3, 2, 4, 5, 3, 2]
>>> print (a)
[1, 4, 3, 2, 4, 5, 3, 2]
>>> b = a.sort()
>>> b
>>> print (a)
[1, 2, 2, 3, 3, 4, 4, 5]
>>>
msg365131 - (view) Author: Zhu Sheng Li (akarei) * Date: 2020-03-27 02:52
I think it's an expected behavior.

`a.sort()` sorts the list itself in place, so it returns None.

If you want a sorted list returned, you need to use `sorted` function.

https://docs.python.org/3/library/functions.html#sorted
msg365133 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-27 05:19
In the future, please use the python-tutor or python-list mailing lists if you need help. If you still think you've found a bug in python, then open an issue here.

https://mail.python.org/mailman/listinfo/tutor
https://mail.python.org/mailman/listinfo/python-list
History
Date User Action Args
2020-03-27 06:01:06terry.reedysetcomponents: - IDLE
2020-03-27 06:00:53terry.reedysetassignee: terry.reedy ->
2020-03-27 05:19:54eric.smithsetstatus: open -> closed

type: resource usage -> behavior

nosy: + eric.smith
messages: + msg365133
resolution: not a bug
stage: resolved
2020-03-27 02:52:16akareisetnosy: + akarei
messages: + msg365131
2020-03-27 02:26:01Sivasundar Nagarajancreate