Differences between sorted and sort in python

preview_player
Показать описание
in python, the terms `sorted` and `sort` are both related to sorting elements in a list, but they are used in different ways.

1. `sorted()`: this is a built-in function in python that returns a new sorted list from the elements of any iterable (e.g., list, tuple, string). the original list remains unchanged.

2. `sort()`: this is a method that can be called on a list object directly. it sorts the elements of the list in place, meaning the original list is modified.

here is a code example to demonstrate the differences between `sorted` and `sort` in python:

output:

as you can see from the example, `sorted()` returns a new sorted list while leaving the original list unchanged. on the other hand, `sort()` sorts the list in place, modifying the original list itself.

...

#python difference between ' and
#python difference between two datetimes
#python difference between list and array
#python differences between two lists
#python difference

python difference between ' and
python difference between two datetimes
python difference between list and array
python differences between two lists
python difference
python difference between list and tuple
python difference between two dates
python differences between two strings
python differences between two sets
python sorted
python sorted reverse
python sort array
python sort dictionary by key
python sort lambda
python sort list alphabetically
python sort string
python sorted function
python sort dictionary by value
Рекомендации по теме
join shbcf.ru