Learn Python list operations with zero foundation

"The Way of Python Data" Guide: This article comes from a reader's contribution. The Way of Python Data has also posted articles related to the Python list earlier, you can go to view:


Introduction to Python data types-List (Part 1)

Python data types-Introduction to List (Part 2)-List comprehension



Learn Python list operations with zero foundation

Python is an object-oriented programming language, and lists are one of the most powerful Python functions that beginners can use directly. The following editor summarizes the introduction and use of lists in Python, full of dry goods, For everyone to learn.

1. What is a list?

The list is composed of a series of elements arranged in a specific order. The elements in the list can include all the letters in the alphabet, numbers 0-9, Chinese, and so on. There can be no relationship between the elements. In Python, square brackets [] are used to denote a list, and the elements are separated by commas, for example:

names = ['Tom','Jame','Marry']print(names)

2. Access list elements

Simply tell Python the position or index of the element that needs to be accessed to access the list element. Namely list name [element index]:

print(names[0])

The thing to note here is that the index of the first list is 0, not 1. If you want to access the last element, specify the index as -1.

3. Addition, deletion and modification of the list

Most of the creation of the list is dynamic, which means that after the creation of the list, you need to add, delete, and modify elements following the program.

Modify elements

Modifying list elements is similar to accessing list elements, by specifying the list name plus the index of the element to be modified, and then specifying a new value.

names[0] = 'Jack'print(names)

Add element

We can add elements directly to the end of the list, which is also the easiest way. Use the append() method to add the element'Alice' to the end of the list without affecting other elements.

names.append('Alice')

Use append() to create a new list

lists = []lists.append('a')lists.append('b')lists.append('c')print(lists)

Output: ['a','b','c'] We can also use the insert() method to insert new elements at any position in the list. In the following example,'Alice' is added to the beginning of the list, and the other elements move backward one position in turn.

names.insert(0,'Alice')

Delete list

There are three ways to delete a list: use the del statement, use the pop() method, and use the remove() method.

(a) del statement

del names[1] #Delete the second element in the names list

(b) pop() method

name1 = names.pop()

This sentence means that the pop() function pops the last element in the names list and saves this element in name1.

name1 = names.pop(1) #Specify the second element to pop up

(c) remove() method

When we do not know the location of the element to be deleted, we can delete the element value between the remove() function

names.remove('Jack')

The remove() function can only delete the first specified value in the list. If the value appears multiple times in the list, you need to use a loop statement!

4. Sorting the list

The sorting of the list is divided into permanent sorting and temporary sorting, we will explain one by one below:

Permanent sort

Use the sort() method to sort the list permanently, for example:

list1 = ['a','c','b']list1.sort()print(list1)

Output: ['a','b','c'] This function sorts the list permanently, that is, it can no longer be restored to the original order

list1.sort(reverse = True) #reverse order

Temporary sort

To keep the original order of the list elements, you can use the sorted() function, which guarantees that they are arranged in a specific order without affecting their original positions.

print(sorted(list1))print(list1)

Output: ['a','b','c']['a','c','b'] Note that the sorting order of the list elements does not change after calling the sorted() function.

Reverse list

If you want to reverse the order of the list elements, you can use the reverse() method.

list1.reverse()print(list1)

Output: ['b','c','a'] Note that this method does not reverse the alphabetical order, but only reverses the order of the list elements.

5. Traversing the list

Use a for loop to traverse all elements of the list and perform the same operation on each element. For example, to print all names in the list:

names = ['Tom','Jame','Marry']for name in names:    print(name)

6. List comprehension

List comprehension refers to generating the required list with only one line of code, writing a for loop to provide values ​​to the expression, and adding []. E.g:

a = [i**2 for i in range(1,6)]print(a)

Output: 1, 4, 9, 16, 25, which creates a list of square numbers, and the for loop provides 1-5 to the expression i**2.

7. Use the list

Use the specified part of the list elements by slicing

list1 = ['a','b','c','d']list1[0:3] #Print the first three elements of the list list1[1:4] #Print list 2-4 elements list1[2:] #Print the third to the last element of the list list1[:] #Print all list elements

8. Summary

After reading this, I believe you have learned how to create and access a list. We can add list operations to statements such as if, for, while, to achieve various complex programming!

本文来自读者投稿,欢迎大家点击下面链接进行投稿:
欢迎投稿


---------End---------
关注后回复“w”,加我私人微信



image.png


Guess you like

Origin blog.51cto.com/15064628/2599798