Python List 列表 基本定义及增删改查操作

本文详细介绍了Python列表的基本特性,包括声明定义、存储任意类型对象的能力。重点讲解了如何通过索引访问元素,如正索引和负索引的使用。同时,阐述了添加元素的方法,如append()和insert()函数的用法。此外,还讨论了删除元素的操作,如del语句、pop()和remove()函数。最后提到了通过索引修改列表元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Basic Characteristics

A list is a collection of items in a particular order.

Declaration & define – store any object[type]
lt = [1, 1.1, 'str', ['list']] # contain type : [int, float, string, list] 
Basic Operator[CRUD]
Accessing Elements by index
           [1, 1.1, 'str', ['list']]
postive:    0   1     2        3
negative:  -4  -3    -2       -1
Positive Index – index of interval is [0, length), which begin from 0.
print(lt[0]) #output: 1
Negative Index – index of interval is [-lentgh, -1]
# Tiny skill: output last element of list by -1(index), rather than 3(lenght - 1)
print(lt[-1]) # output: ['list']
Adding Elements – append(), insert()
append() – at the end of a list
lt.append('Adding')
print(lt) # output: [1, 1.1, 'str', ['list'], 'Adding']
insert(index, obj) – index is position of adding you want. interval is [0, length) OR [-length, -1]
lt.insert(0, 'insert_0')
lt.insert(-1, 'insert_end(-1)')
print(lt) # output:['insert_0', 1, 1.1, 'str', ['list'], 'insert_end(-1)', 'Adding']
# Is consuqence of output different with you ?
# insert(index, obj) method is backwards move a unit for elements of
# index rihgt-hand and itself.To -1(index), position of end before change. So:
# ['insert_0', 1, 1.1, 'str', ['list'], 'Adding']
#                                          -1
# ['insert_0', 1, 1.1, 'str', ['list'], 'insert_end(-1)', 'Adding']
#                                              | 
#                                       insert of position
Remove Elements – del, pop(), remove()
del statement — certain position by index
del lt[-1]
print(lt) # output: ['insert_0', 1, 1.1, 'str', ['list'], 'insert_end(-1)']
pop() & pop(index) – remove with return-value
# like stack -- DS
re_value = lt.pop()
print(re_value)	# output: insert_end(-1)
print(lt) # ['insert_0', 1, 1.1, 'str', ['list']]
remove(value) – by value
lt.remove(1)
print(lt) # output: ['insert_0', 1.1, 'str', ['list']]
Change Elements – by index
lt[-2] = 'string' # change 'str' to 'string'
print(lt) # output: ['insert_0', 1.1, 'string', ['list']]
Code Summary
lt = [1, 1.1, 'str', ['list']] # contain type : [int, float, string, list]

print(lt[0]) #output: 1

# Tiny skill: output last element of list by -1(index), rather than 3(lenght - 1)
print(lt[-1]) # output: ['list']

lt.append('Adding')
print(lt) # output: [1, 1.1, 'str', ['list'], 'Adding']

lt.insert(0, 'insert_0')
lt.insert(-1, 'insert_end(-1)')
print(lt) # output:['insert_0', 1, 1.1, 'str', ['list'], 'insert_end(-1)', 'Adding']

del lt[-1]
print(lt) # output: ['insert_0', 1, 1.1, 'str', ['list'], 'insert_end(-1)']

# like stack -- DS
re_value = lt.pop()
print(re_value)
print(lt)

lt.remove(1)
print(lt)

lt[-2] = 'string' # change 'str' to 'string'
print(lt)

Environment
  • Ubuntu18.x
  • python 3.6.9
Reference
QuickLink