Telegram Group & Telegram Channel
What is the difference between list.sort() and sorted() in Python?

Python
provides two ways to sort a list, the built-in list method list.sort() and the built-in function sorted(). Although both will sort the elements of a list, if used incorrectly they can produce unexpected or undesired results.

Differences and similarities

The primary difference between the two is that list.sort() will sort the list in-place, mutating its indexes and returning None, whereas sorted() will return a new sorted list leaving the original list unchanged. Another difference is that sorted() accepts any iterable while list.sort() is a method of the list class and can only be used with lists.


Example:

nums = [2, 3, 1, 5, 6, 4, 0]

print(sorted(nums)) # [0, 1, 2, 3, 4, 5, 6]
print(nums) # [2, 3, 1, 5, 6, 4, 0]

print(nums.sort()) # None
print(nums) # [0, 1, 2, 3, 4, 5, 6]

Both list.sort() and sorted() have the same key and reverse optional arguments and can be called on each list element prior to making comparisons.


When to use each one:

list.sort() should be used whenever mutating the list is intended and retrieving the original order of the elements is not desired. On the other hand, sorted() should be used when the object to be sorted is an iterable (e.g. list, tuple, dictionary, string) and the desired outcome is a sorted list containing all elements.

Share and Support
@Python_Codes



tg-me.com/python_codes/157
Create:
Last Update:

What is the difference between list.sort() and sorted() in Python?

Python
provides two ways to sort a list, the built-in list method list.sort() and the built-in function sorted(). Although both will sort the elements of a list, if used incorrectly they can produce unexpected or undesired results.

Differences and similarities

The primary difference between the two is that list.sort() will sort the list in-place, mutating its indexes and returning None, whereas sorted() will return a new sorted list leaving the original list unchanged. Another difference is that sorted() accepts any iterable while list.sort() is a method of the list class and can only be used with lists.


Example:

nums = [2, 3, 1, 5, 6, 4, 0]

print(sorted(nums)) # [0, 1, 2, 3, 4, 5, 6]
print(nums) # [2, 3, 1, 5, 6, 4, 0]

print(nums.sort()) # None
print(nums) # [0, 1, 2, 3, 4, 5, 6]

Both list.sort() and sorted() have the same key and reverse optional arguments and can be called on each list element prior to making comparisons.


When to use each one:

list.sort() should be used whenever mutating the list is intended and retrieving the original order of the elements is not desired. On the other hand, sorted() should be used when the object to be sorted is an iterable (e.g. list, tuple, dictionary, string) and the desired outcome is a sorted list containing all elements.

Share and Support
@Python_Codes

BY Python Codes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_codes/157

View MORE
Open in Telegram


Python Codes Telegram | DID YOU KNOW?

Date: |

How to Use Bitcoin?

n the U.S. people generally use Bitcoin as an alternative investment, helping diversify a portfolio apart from stocks and bonds. You can also use Bitcoin to make purchases, but the number of vendors that accept the cryptocurrency is still limited. Big companies that accept Bitcoin include Overstock, AT&T and Twitch. You may also find that some small local retailers or certain websites take Bitcoin, but you’ll have to do some digging. That said, PayPal has announced that it will enable cryptocurrency as a funding source for purchases this year, financing purchases by automatically converting crypto holdings to fiat currency for users. “They have 346 million users and they’re connected to 26 million merchants,” says Spencer Montgomery, founder of Uinta Crypto Consulting. “It’s huge.”

The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.

Python Codes from no


Telegram Python Codes
FROM USA