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 :: how to make a new df from old 
Python :: Default rows values display 
Python :: square root in python numpy 
Python :: number of libraries in python 
Python :: xgb plot importance round 
Python :: how to limit variable godot 
Python :: saving a dta file 
Python :: split list in two based on condition python 
Python :: print same index and value on each iteration of the for loop in Python 
Python :: find factors of a number using while loop 
Python :: Python String count() Implementation of the count() method using optional parameters 
Python :: Example of Python Strings with indexing 
Python :: python faq call by reference 
Python :: copy director structure python 
Python :: python comment faire une boucle 
Python :: strain rate 
Python :: OSError Traceback (most recent call last) <ipython-input-74-8920269c5588 in <module() 9 10 run_with_ngrok(app) --- 11 app.run() 
Python :: Loading data from Oracle Database to pandas DataFrames 
Python :: Python NumPy atleast_3d Function Example 
Python :: Django merge duplicate rows 
Python :: k means em algorithm program in python 
Python :: Python NumPy dstack Function Example 01 
Python :: Python NumPy hsplit Function Syntax 
Python :: __sub__ 
Python :: using Canvas with tkinger 
Python :: NumPy left_shift Code When inputs and bit shift are an arrays 
Python :: numpy image processing 
Python :: green book résumé 
Python :: how to process numerical data machine learning 
Python :: qmenu 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =