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 :: assignment 7.1 python data structures 
Python :: how to get the first few lines of an ndarray 3d 
Python :: how to get pygame key 
Python :: how to check if an input is a string in python 
Python :: python scanner class 
Python :: path of current working directory with os module python 
Python :: import word_tokenize 
Python :: python odbc access database 
Python :: count the number of rows in a database table in Django 
Python :: redirect if not logged in django 
Python :: delete spaces in string python 
Python :: sqlite check if table exists 
Python :: python convert bool to string 
Python :: discord python webhook 
Python :: pickling and unpickling in python 
Python :: python convert dict to xml 
Python :: sqlalchemy create engine Microsoft SQL 
Python :: how to determine python project parent dir 
Python :: python to create pandas dataframe 
Python :: ValueError: `logits` and `labels` must have the same shape, received ((None, 2) vs (None, 1)). 
Python :: axes color python 
Python :: python logger get level 
Python :: continual vs continuous 
Python :: api testing with python 
Python :: python try except raise error 
Python :: object literal python 
Python :: sort dict by values 
Python :: create a dictionary in python 
Python :: concatenate 2 array numpy 
Python :: pandas write to excel 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =