The list of

  • The variable used to store multiple data is a list
# define list
students = ["Zhang"."Bill"."Fifty"]
Copy the code

Access list

# define list
students = ["Zhang"."Bill"."Fifty"]
Access list
print(students[0])Access the first element of the list
print(students[1])Access the second element of the list
print(students[2])Access the third element of the list
Copy the code

Results of the above code:

Traverse the list

# define list
students = ["Zhang"."Bill"."Fifty"]
# walk through the list
for i in students:
    print(i)

Print (students[I]) I +=1 "print(students[I]) I +=1" print(students[I]) I +=1"
Copy the code

Results of the above code:

Common List Operations

Add elements

  • append()
# define list
students = ["Zhang"."Bill"."Fifty"]
#append() adds the element to the end
students.append("Xiao Ming")# Add Xiao Ming to the list
print(students)
Copy the code

Results of the above code:

  • extend()
# define list
students = ["Zhang"."Bill"."Fifty"]
#extend() breaks apart another list and inserts it separately into the list
small_students = ["Xiao Ming"."Little red"."Xiao LAN"]
students.extend(small_students)
print(students)
Copy the code

Results of the above code:

  • insert()
# define list
students = ["Zhang"."Bill"."Fifty"]
#insert() inserts into the specified position according to the subscript
students.insert(0."Xiao Ming")Insert Ming into the first data
print(students)
Copy the code

Results of the above code:

Look for the element

# define list
students = ["Zhang"."Bill"."Fifty"]
# Suppose you want to find Li Si
#in Determines if an element is in the list
#index Is used to find the index of an element
name = "Bill"
if name in students:
    print("Li Si here")
    print("Li Si index is %d"%students.index(name))
else:
    print("Li Si is not here.")
Copy the code

Results of the above code:

Modify the element

# define list
students = ["Zhang"."Bill"."Fifty"]
students[1] = "Xiao Ming"# Change Li Si into Xiao Ming
Copy the code

Results of the above code:

Remove elements

  • remove()
# define list
students = ["Zhang"."Bill"."Fifty"]
#remove Removes the element by name
students.remove("Fifty")
print(students)
Copy the code

Results of the above code:

  • pop()
# define list
students = ["Zhang"."Bill"."Fifty"]
#pop() removes elements according to the index
The last data is deleted by default
students.pop(0)Delete the first data
students.pop()Delete the last data
print(students)
Copy the code

Results of the above code:

  • del
# define list
students = ["Zhang"."Bill"."Fifty"]
#del Deletes data based on the list index
del students[2]Select * from index 2
print(students)
Copy the code

Results of the above code:

statistical

  • count()
# define list
students = ["Zhang"."Bill"."Fifty"."Bill"]
#count Count of elements
print("There are % D of lees."%students.count("Bill"))
Copy the code

Results of the above code:

The sorting

  • ascending
Define a list of class scores
students = [67.89.32.56.100.34]
#sort(
students.sort()
print(students)
Copy the code

Results of the above code:

  • Descending order
Define a list of class scores
students = [67.89.32.56.100.34]
#sort(reverse = True) descending order
students.sort(reverse = True)
print(students)
Copy the code

Results of the above code:

  • Reverse order
Define a list of class scores
students = [67.89.32.56.100.34]
#reverse(
students.reverse()
print(students)
Copy the code

Results of the above code:

Nested list

There are three groups with two members in each group
group = [["Little red"."Xiao Ming"], ["Zhang"."Bill"], ["Wang"."Odbo"]]
# traversal
for i in group:# I stands for each group
    for j in i:#j stands for group
        print(j)
# Find members
print(group[0] [1])Find the second member of the first group
Copy the code

Results of the above code: