“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

The 27th official Python column, stop! Don’t miss this zero-based article!

In the previous article, we listed the corresponding operations of the list data according to the tuple article.

Let’s continue through the rest of the list this time.

List has more support for these operations than tuple

As mentioned earlier, tuple is a string of welded carriages. List supports element editing, which is obviously much more flexible.

Let’s take a look at deleting. Python’s List supports deleting elements as shown in 3.

Given that we define a list object list_obj, we can perform either of the following operations to remove the element.

Del list_obj[subscript] list_obj. Remove (subscript) list_obj. Pop (subscript) #Copy the code

Ok, let’s look at the complete code below:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: list_demo4.py # @Project: Print ("mylist:", mylist) del mylist[0] print("after remove first element, Mylist :", mylist) mylist. Remove (") print("after remove first element, mylist:", Print ("after remove first element, mylist:", mylist) print("removed value:", removed_value)Copy the code

The effect is as follows:

Delete elements that exceed the subscript range of the list.

How do you add/extend elements to a list other than deleting them?

Not to mention modification, list also supports locating lookup elements, so let’s take a look.

List_obj.index # select * from list_obj.index where list_obj.index = 0Copy the code

List_obj = [3, 2, 1] list_obj.index(2)

The answer is: 1.

Ok, so let’s go ahead and insert new elements.

List_obj. insert(specify subscript, element) list_obj.append(elementCopy the code

What about appending multiple lists at once, or simply extending a list to an existing list?

Extend extends list_obj. Extend extends list_obj directly, appending the elements of the list to the end.

With all that said, let’s just copy and run the following code:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: list_demo5.py # @Project: Mylist = [6, 6, 6] print("mylist:", mylist) mylist.append(" mylist:") print("mylist:", mylist) print("mylist:", mylist) ", mylist.count(6)) print(" ", mylist.index(6)) mylist.insert(2, 1024) print(" ", mylist.index(1024)) last = mylist.pop() Print (" The last element is: Mylist = mylist * 2 print("mylist:", mylist) Mylist) mylist.extend([' mylist ', 'mylist ']) print("mylist:", mylist)Copy the code

This is what the code looks like:

Very simple, let’s go ahead and look at sorting list elements

The list of the sort

All of this is editing, but you can also sort the data in a list, in a logical order.

List provides a sort function and a reverse function.

The reverse function is the equivalent of turning the entire train car around. List :[1,2,3] becomes [3,2,1] after being processed by the reverse function.

Sort is more flexible, installing element literals (such as numbers, numeric strings) by default, and passing in a lambda function that specifies the sorting logic.

By default, the above functions sort an array of numbers by numeric value

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: list_demo6.py # @Project: Mylist = [2, 3, 1] # myList = ["2", "3", "1"] mylist.sort() print(" myList :", Print ("mylist:", mylist) mylist.sort(key=lambda e: len(str(e)), reverse=True) print("sorted mylist:", mylist) mylist.reverse() print("reversed mylist:", mylist)Copy the code

The effect is as follows, and the reader can take a closer look to see if it is as the committee says.

Note: As the sample code above also shows, if the elements in a list are not of the same type (all numbers, all strings, or all of a certain type), the developer must implement a lambda function to sort by reference to the sort function.

conclusion

List has a lot of functionality, and it’s very convenient to do things like add/remove/locate elements.

In addition, it is easy to expand, sort, reverse order, etc., which makes the list very widely used. Everyone who is learning Python must type code more and master it well.

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!