Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sorted vs sort python

# The sort() function will modify the list it is called on. 
# The sorted() function will create a new list 
# containing a sorted version of the list it is given.

list = [4,8,2,1]
list.sort()
#--> list = [1,2,4,8] now

list = [4,8,2,1]
new_list = list.sorted()
#--> list = [4,8,2,1], but new_list = [1,2,4,8]
Comment

sort vs sorted python

>>> sorted((3,7,4,9,2,1,7))
[1, 2, 3, 4, 7, 7, 9]
>>> sorted({'one':1, 'two':2,'three':3,'four':4,'five':5})
['five', 'four', 'one', 'three', 'two']
>>> sorted([3,7,4,9,2,1,7])
[1, 2, 3, 4, 7, 7, 9]
>>> sorted('this is a string')
[' ', ' ', ' ', 'a', 'g', 'h', 'i', 'i', 'i', 'n', 'r', 's', 's', 's', 't', 't']
>>>
Comment

python sorted vs sort

numbers = [2,3,5,10,1]
# sorted(numbers): creates a new list, without modifying the numbers list
# numbers.sort(): modifies the numbers list


sorted_numbers = sorted(numbers)
#--> numbers: 		 [2,3,5,10,1]
#--> sorted_numbers: [1,2,3,5,10]

numbers.sort()
#--> numbers: 		 [1,2,3,5,10]
Comment

PREVIOUS NEXT
Code Example
Python :: Encapsulation in Python using public members 
Python :: tkinter label abstand nach oben 
Python :: python class private variables 
Python :: dickyfuller test in python 
Python :: is console and terminal is same in spyder python(3.9) 
Python :: factorielle python 
Python :: center fig legend 
Python :: numpy array values not updateing 
Python :: python project pick text color according to background 
Python :: repalce na with mean per group 
Python :: islink(node1 node2) is used for 
Python :: sumif in python on a column and create new column 
Python :: pdfkit supress output 
Python :: falcon 900 price 
Python :: what takes more memory string or list python 
Python :: python how to get variable value in dict 
Python :: checking number of connected users hotspot ubuntu 
Python :: how to select the shortest item in a python list 
Python :: fibonacci formula python 
Python :: start models 
Python :: python iterate through list by chunks 
Python :: updating to database 
Python :: Python docx title 
Python :: use an async check function for discord.py wait_for? 
Python :: Lazada link 
Python :: return Response converting to string drf 
Python :: python autoLibrary 
Python :: python abbreviated for loop 
Python :: average values in a list python 
Python :: saving a dta file 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =