Sort the List
The List object in Python has a built-in sort() method that naturally sorts the elements of the List alphabetically/numerically in ascending order, as shown in the following code:
thislist = ["orange"."mango"."kiwi"."pineapple"."banana"]
thislist.sort()
print(thislist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['banana'.'kiwi'.'mango'.'orange'.'pineapple']
Copy the code
If the list is all numbers, the default is to sort the list in ascending order, as shown in the following code:
thislist = [100.50.65.82.23]
thislist.sort()
print(thislist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
[23.50.65.82.100]
Copy the code
The List in descending order
In many cases, we also need to reverse. To reverse, we need to add reverse = True to sort() as follows:
thislist = ["orange"."mango"."kiwi"."pineapple"."banana"]
thislist.sort(reverse = True)
print(thislist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['pineapple'.'orange'.'mango'.'kiwi'.'banana']
Copy the code
In the same way, let’s look at a list of pure numbers.
thislist = [100.50.65.82.23]
thislist.sort(reverse = True)
print(thislist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
[100.82.65.50.23]
Copy the code
Copy List
Note that Copy lists cannot simply use list2 =list1, so that list1 and list2 are the same reference, and modifying one List affects the other.
There are two ways to copy a list.
- Use copy()
thislist = ["apple"."banana"."cherry"]
mylist = thislist.copy()
print(mylist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'banana'.'cherry']
Copy the code
- Use the list() function
Put an existing list into the constructor of a new list as follows:
thislist = ["apple"."banana"."cherry"]
mylist = list(thislist)
print(mylist)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'banana'.'cherry']
Copy the code
The List with
There are several ways to combine lists in Python.
- Use the +.
This also seems to be unique to Python, with the following code:
list1 = ["a"."b"."c"]
list2 = [1.2.3]
list3 = list1 + list2
print(list3)
PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['a'.'b'.'c'.1.2.3]
Copy the code
- Use the append() function
Using the append() function to append each item in a collection individually to the new collection is a bit more cumbersome, as shown in the following code:
list1 = ["a"."b" , "c"]
list2 = [1.2.3]
for x in list2:
list1.append(x)
print(list1)
Copy the code
- Use the extend() function
list1 = ["a"."b" , "c"]
list2 = [1.2.3]
list1.extend(list2)
print(list1)
Copy the code
More List methods
There are a lot of methods in the List. You can operate other methods by yourself.
Link: www.w3schools.com/python/pyth…
For more high-quality dry goods: See my GitHub:python