Take you to learn the basics of python: List

Posted by opencombatclan on Thu, 09 Dec 2021 00:51:06 +0100

In this section, we will learn more about List operations in more detail!

1, What is the list?

What a list is: a box can hold all kinds of things, and the box is equivalent to this list, which is what can hold all kinds of data in the program.

To put it professionally, a list is composed of a series of elements arranged in a specific order. A list can store many types of data, and there can be no relationship between the elements

2, How to access lists

A List is a box. The features of other languages are equivalent to arrays or lists in Java.

About how to access the list, it's natural.

The above figure can be regarded as a list, which contains Ouyang Sihai, 18, Wuhan and 1.75 data respectively.

Access list

Method: it is the same as accessing an array. For example, if there is a List, you only need to access: List[0], which is to access the first element.

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Access list
first = List1[0]
print(first)

second = List1[1]
print(second)

3, Action list

3.1 list addition, deletion, modification and query

First, take the above figure as an example

Add element

First, add an element at the end of the list, where the append method is used.

We add a 'hello world' at the end of the list, and the results are as follows

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Add an element at the end of the list
List1.append('hello world')

# Access list
print(List1[4])

Sometimes, we may need to add elements at any location. At this time, we can use the insert method.

For example, we need to add an element 'is' after the first position.

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Add elements anywhere
List1.insert(2, 'is')
print(List1[2])

Delete element

python provides three ways to delete elements, but the purpose of each method is different.

  • Del statement delete element: if you know the position of the element to be deleted in the list, you can use del statement to delete the element. Once the element is deleted, it can no longer be accessed
def printValue(List1):
    '''
    Print list elements
    :return:
    '''
    for value in List1:
        print(value)

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Delete elements using del
del List1[0]

# Print element
printValue(List1)

  • pop() method deletes an element: the pop method is used to remove an element from the list (the default is the last element). You can specify the element index and return the value of the element.

Welcome, thank you for your attention to the official account of python tutorial. Finally wait for you! We have prepared a free python crawler course and learning gift bag for you!

def printValue(List1):
    '''
    Print list elements
    :return:
    '''
    for value in List1:
        print(value)

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Delete elements using pop()
List1.pop()  # Delete last by default

# Print element
printValue(List1)

List1.pop(0)  # Specify location

# Print element
print('*' * 10)
printValue(List1)

  • remove() method deletes the element: when you do not know the element index but only the element value, use the remove() method to delete the element
def printValue(List1):
    '''
    Print list elements
    :return:
    '''
    for value in List1:
        print(value)

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Use remove() to delete elements
List1.remove('Norway')

printValue(List1)

Modify element
Modifying elements is very simple. Just like arrays, you can modify them directly!

def printValue(List1):
    '''
    Print list elements
    :return:
    '''
    for value in List1:
        print(value)

# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Modify element
List1[0] = 'Vegetable chicken'

printValue(List1)

Find element

Previously, we can find the element at the corresponding position by subscript. Now we want to see whether an element is in this list.

  • in (exists). If it exists, the result is true; otherwise, it is false
  • not in (does not exist). If it does not exist, the result is true; otherwise, it is false
# Create a list
List1 = ['Norway', 18, 'wuhan', 1.75]

# Find whether the element is in the list
item = 'Norway'

flag = item in List1
flag2 = item not in List1
print(flag)
print(flag2)

4, List method

Here are some tables. Just read them.

5, Summary

In this section, we mainly talk about the access, addition, deletion and modification of the List, as well as the use of the related functions of the List!

Advantages of learning Python / Crawler / artificial intelligence well
1. The university tuition and living expenses shall be borne by themselves, and the economy is more independent and free;
2. Do Python, crawler and choose a new direction in the future;
3. Let yourself have a complete personality, full of self-confidence and live a wonderful life; Add > > > [learn Python now] Norwegian micro number: nuow9880

Topics: Python crawler Data Analysis list