Python3 list

Python3 list

A list is the most basic data structure in Python and the most commonly used Python data type. It can appear as a comma-separated value in square brackets. Each element in the list is assigned a number-that is its subscript, or index, the first index always starts at 0, the second index is 1, and so on. A list is also called a sequence, which is similar to the concept of an array, except that different types of data can be placed in a list, similar to the Object collection in Java, so the data items of the list do not need to have the same type, and the list The size can be automatically scaled, which is the same as the concept of a collection.
To create a list, simply enclose the different data items separated by commas in square brackets. Code example:

list1 =['Google','Runoob',1997,2000];
list2 =[1,2,3,4,5];
list3 =["a","b","c","d"];

Like the index of a string, the list index starts at 0. The list can be intercepted, combined, etc.

Access the value in the list

Use the subscript index to access the values in the list. Similarly, you can also use square brackets to intercept the elements in the list. Code example:

list1 =['hello','world',123,456]
list2 =[1,2,3,4,5,6,7,8,9]print("The 0th subscript value of list1 is:", list1[0])print("list2 subscript 0-Values within 5 are:", list2[0:5])

operation result:

The 0th subscript value of list1 is: hello
The values within subscript 0-5 of list2 are: [1, 2, 3, 4, 5]

update list

You can modify or update the data items of the list. The so-called update is to re-assign a certain subscript in the list. After the re-assignment, the original value will be overwritten. Code example:

list1 =['hello','world',123,456]print("The 0th subscript value of list1 is:", list1[0])
list1[0]="Hello there"print("The 0th subscript of list1 after the update is:", list1[0])

operation result:

The 0th subscript value of list1 is: hello
After the update, the 0th subscript value of list1 is: Hello

Delete list element

You can use the del statement to delete elements of the list, code example:

list1 =['hello','world',123,456]print("Now the elements in list1 are:",list1)
del list1[1]print("After deleting the first element:",list1)

operation result:

Now the elements in list1 are: ['Hello','world', 123, 456]
After deleting the first element: ['Hello', 123, 456]

List script operator

The operators of the list pairs + and * are similar to strings. The + sign is used for combined lists, and the * sign is used for repeated lists.
List interception and splicing code example:

list1 =['hello','world',123,456]

# Intercept
print(list1[2])print(list1[-2])print(list1[1:])

operation result:
456123[123,456]

# Stitching, can only be used in the python console
>>> list1 =['hello','world',123,456]>>> list1+[1,2,3,4,5,6,7,8,9]['hello','world',123,456,1,2,3,4,5,6,7,8,9]

Two-dimensional list

A two-dimensional list means that there are also lists in the list. Using a two-dimensional list means to create other lists in the list. The following examples illustrate two commonly used two-dimensional list declaration methods and code examples:

list1 =['hello','world',123,456]
list2 =[1,2,3,4,5,6,7,8,9]
list3 =[list1, list2]print(list3)

list4 =[['hello','world',123,456],[1,2,3,4,5,6,7,8,9]]print(list4)

operation result:

[[‘hello’, ‘world’, 123, 456], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
 [[‘hello’, ‘world’, 123, 456], [1, 2, 3, 4, 5, 6, 7, 8, 9]]

Two-dimensional lists are not used much, and most of them are used to make 2D game maps

Python list functions & methods

The following uses actual code to demonstrate several more commonly used methods, code examples:

list1 =['hello','world',123,456,123,'hello']
list2 =[45,12,78,56,3,2,48,78,156,45,1]

list1.append('addObj')print("A new value is added at the end:", list1)

list1.insert(2,'InsertObj')print("A new value is inserted at the position of subscript 2:", list1)print("The first index position of the value 123:", list1.index(123))

list1.remove('hello')print("Delete the first'hello' in the list:", list1)

list2.sort()print("Sorted list2:", list2)

list2.reverse()print("List2 after reverse sorting:", list2)

list3 = list1.copy()print("Copy the data of list1 to list3:", list3)

list3.clear()print("Clear the data in list3:", list3)

operation result:

A new value is added at the end: ['hello','world', 123, 456, 123,'hello','addObj']
A new value is inserted in the position of subscript 2: ['hello','world','InsertObj', 123, 456, 123,'hello','addObj']
The first index position of the value 123: 3
Delete the first'hello' in the list: ['world','InsertObj', 123, 456, 123,'hello','addObj']
Sorted list2: [1, 2, 3, 12, 45, 45, 48, 56, 78, 78, 156]
List2 after reverse sorting: [156, 78, 78, 56, 48, 45, 45, 12, 3, 2, 1]
Copy the data of list1 to list3: ['world','InsertObj', 123, 456, 123,'hello','addObj']
Clear the data in list3: []

Recommended Posts

Python3 list
python list learning
[902] python list sort
In-depth understanding of python list (LIST)
Python basic syntax list production
Python multithreading
Implementation of reverse traversal of python list
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python list comprehension operation example summary
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python toolchain
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Python randomly shuffles the elements in the list
Python answers questions
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python string manipulation
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
Explain the list under Python multithreading in detail
Python review one
linux+ubuntu solve python
Python learning-variable types
7 features of Python3.9