Get the App
SLTechnology News&Howtos  ›  Development  › 

What is Python list sorting

Shulou Source: shulou.com Published: 2022-06-03 07:20:03 11月15日 Update

This article mainly explains "what is the sort of Python list". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what is the sort of Python list.

In Python, there are two ways to sort lists.

One is to call the sort () method, which returns no value and sorts the list itself in ascending order.

Cars = ['bmw',' audi', 'toyota',' subaru'] cars.sort () print (cars)

Output:

['audi',' bmw', 'subaru',' toyota']

Another way is to use the sorted () function, which returns the list sorted in ascending order without affecting the original list.

Cars = ['bmw',' audi', 'toyota',' subaru'] print ("Here is the original list:") print (cars) print ("\ nHere is the sorted list:") print (sorted (cars)) print ("\ nHere is the original list again:") print (cars)

Output:

Here is the original list: ['bmw',' audi', 'toyota',' subaru'] Here is the sorted list: ['audi',' bmw', 'subaru',' toyota'] Here is the original list again: ['bmw',' audi', 'toyota',' subaru'] Thank you for reading. That's what Python list sorting is. After the study of this article, I believe that you have a deeper understanding of what is the sort of Python list, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Sort method learning content function ascending order output original at the same time that is ideas situations articles more knowledge knowledge points articles follow questions practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Apple Shulou Information Linux