[Python] Difference between sorted and sorted (Colaboratory)

[Python] Difference between sorted and sorted

About the difference between sorted and sorted in python. Both are algorithms that sort in ascending order.

Is the difference destructive?

sort: Destructive (the original data changes) sorted: Do not destroy (original data remains)

python


#In case of sort
a = [2,3,4,5,1]
a.sort()
print(f'a:{a}')

#a:[1, 2, 3, 4, 5]

sort changes the original data itself.

python


#In case of sorted
b = [2,3,4,5,1]
c = sorted(b)
print(f'b:{b}')
print(f'c:{c}')

#b:[2, 3, 4, 5, 1]
#c:[1, 2, 3, 4, 5]

In sorted, the original data remains the same.


By the way, ** sort is a method, sorted is a built-in function **.
## What is Colaboratory? An online python analysis tool provided by google. Useful when checking the result of python code execution.

Jupyter Notebook is also convenient, but it takes time to install. Colaboratory is easy because it just opens the page.

https://colab.research.google.com/notebooks/welcome.ipynb?hl=ja

・ Create a new notebook image.png

・ You can add codes and text boxes with + at the top of the screen. image.png

Code execution is Shift + Enter

You can share it with other people as well as Spushi. It is convenient to be able to write a comment in the box.

Recommended Posts

[Python] Difference between sorted and sorted (Colaboratory)
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between == and is in python
Difference between sort and sorted (memorial)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
[Python] Difference between randrange () and randint ()
difference between statements (statements) and expressions (expressions) in Python
Difference between PHP and Python finally and exit
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
[python] Difference between variables and self. Variables in class
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Difference between SQLAlchemy filter () and filter_by ()
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Memorandum (difference between csv.reader and csv.dictreader)
About python dict and sorted functions
List concatenation method in python, difference between list.extend () and “+” operator
(Note) Difference between gateway and default gateway
Cooperation between python module and API
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between SQLAlchemy flush () and commit ()
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
File open function in Python3 (difference between open and codecs.open and speed comparison)
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
[Xg boost] Difference between softmax and softprob
[Django ORM] Difference between values () and only ()
Differences in syntax between Python and Java
Difference between linear regression, Ridge regression and Lasso regression
Difference between docker-compose env_file and .env file
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
speed difference between wsgi, Bottle and Flask
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between numpy.ndarray and list (dimension, size)
Correspondence between Python built-in functions and Rust
Difference between ls -l and cat command
Exchange encrypted data between Python and C #
Difference and compatibility verification between keras and tf.keras # 1
[Introduction to Python] What is the difference between a list and a tuple?
What is the difference between `pip` and` conda`?
Summary of the differences between PHP and Python