In Python, a list is an ordered data collection that can contain any type of data, including integers, floating point numbers, strings, etc. Lists are mutable, which means you can add, remove, or modify elements in them at any time.
Here are some basic operations on lists in Python:
Create list
You can use square brackets []
to create a list and put elements in it, separated by commas.
my_list = [1, 2, 3, 'hello', 5.0]
Access list elements
You can access elements in a list by index, which starts at 0. Negative indexes count down from the end of the list.
my_list = [1, 2, 3, 'hello', 5.0]
print(my_list[0]) # 输出 1
print(my_list[-1]) # 输出 5.0
Modify list elements
The elements in a list are mutable, you can modify them by indexing.
my_list = [1, 2, 3, 'hello', 5.0]
my_list[3] = 'world'
print(my_list) # 输出 [1, 2, 3, 'world', 5.0]
Add element
You can add an element to the end of the list using append()
the method, or insert()
insert an element at a specified position using the method.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # 输出 [1, 2, 3, 4]
my_list.insert(1, 5)
print(my_list) # 输出 [1, 5, 2, 3, 4]
Delete element
You can use del
the keyword, remove()
method, or pop()
method to remove elements from a list.
my_list = [1, 2, 3, 4]
del my_list[1]
print(my_list) # 输出 [1, 3, 4]
my_list.remove(3)
print(my_list) # 输出 [1, 4]
popped_element = my_list.pop() # 默认移除最后一个元素
print(popped_element) # 输出 4
slice
You can use slicing to access subsets of a list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
subset = my_list[2:5] # 从索引2开始,到索引5之前(不包括5)
print(subset) # 输出 [2, 3, 4]
subset = my_list[:5] # 从头开始,到索引5之前(不包括5)
print(subset) # 输出 [0, 1, 2, 3, 4]
subset = my_list[5:] # 从索引5开始到末尾
print(subset) # 输出 [5, 6, 7, 8, 9]
Other common operations
There are some other commonly used list operations, such as getting the length of the list ( len()
), checking whether an element is in the list ( in
keyword), concatenating lists ( +
operator), etc.
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # 输出 5
print(3 in my_list) # 输出 True
print(6 in my_list) # 输出 False
new_list = my_list + [6, 7, 8]
print(new_list) # 输出 [1, 2, 3, 4, 5, 6, 7, 8]
These are some basic operations on lists that provide a convenient way of working with data collections in Python. List is one of the very flexible and commonly used data types.
Learn to test code
"""
# -*- coding: utf-8 -*-
# @Time : 2023/9/18 16:02
# @Author : 王摇摆
# @FileName: tuple.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/weixin_44943389?type=blog
"""
if __name__ == '__main__':
classmates = ['wang', 'pei', 'liu']
print(classmates)
# 获取列表的长度
print(len(classmates))
# 使用索引访问列表元素
print(classmates[0])
# 快速访问列表的最后一个元素
print(classmates[-1])
print(classmates[-2])
# 向列表中追加元素
classmates.append(1)
classmates.append('ABC')
print('\n'+str(classmates))
# 删除列表中的末尾元素
classmates.pop()
print(classmates)
Test Results
D:\ANACONDA\envs\pytorch\python.exe C:/Users/Administrator/Desktop/Code/Learn_Pyhon3.7/liaoxuefeng/tuple.py
['wang', 'pei', 'liu']
3
wang
liu
pei
['wang', 'pei', 'liu', 1, 'ABC']
['wang', 'pei', 'liu', 1]
Process finished with exit code 0