The method of increasing python's list list

Table of contents

1. Usage and examples of append method

(1) Syntax: list.append(object)

(2) Usage: You can insert the value of the target at the end of the original list.

(3) Examples

① Simple usage

②Used in combination with the for function

(4) Error reporting

2. Usage and examples of extend method

(1) Syntax: list.extend(object)

(2) Usage: You can insert the value of the target list at the end of the original list.

(3) Examples

① Simple usage

3. Usage and examples of insert method

(1) Syntax: list.insert(object)

(2) Usage: You can insert the target value at the specified position in the original list.

(3) Examples

① Simple usage

②Use together with for function and dictionary dict


1. Usage and examples of append method

(1) Syntax: list.append(object)

object: Inserting an object means adding a value to the end of the existing list list. A null value cannot be inserted. No result is returned, but the original list value will be modified, and the original list can be called to view it.

(2) Usage: You can insert the value of the target at the end of the original list.

(3) Examples

① Simple usage

#append
#定义列表
list1 = ['love','peace','keep']
list1.append('wish')  #用append函数方法是无返回值需要调佣查看
list1  #jupyter直接调用就行,但是用pycharm软件需要用print函数调用输出


#输出结果为:
#['love', 'peace', 'keep', 'wish']

②Used in combination with the for function

#定义列表
list1 = ['love','peace','keep']
list2 = ['beautiful','string']
for i  in list2:
    #print(i) #beautiful    string
    list1.append(i)
list1


#输出结果为:['love', 'peace', 'keep', 'beautiful', 'string']
#这个结合效果和extend方法结果一样  可参考extend方法的实例

(4) Error reporting

#append
#定义列表
list1 = ['love','peace','keep']
list1.append()
list1
TypeError             
                    Traceback (most recent call last)
<ipython-input-1-a06c86a8d3c8> in <module>
#定义列表
list1 = ['love','peace','keep']
list1.append()
list1

TypeError: append() takes exactly one argument (0 given)

Note: The value to be inserted cannot be empty.


2. Usage and examples of extend method

(1) Syntax: list.extend(object)

object: Inserting an object refers to appending a list value to the end of an existing list list. No result is returned, but the original list value will be modified, and the original list can be called to view it.

(2) Usage: You can insert the value of the target list at the end of the original list.

(3) Examples

① Simple usage

#extend
#定义列表
list1 = ['love','peace','keep']
list2 = ['beautiful','string']
list1.extend(list2) #不返回值,需要调用查看,比如在jupyter界面直接调用输入为list1
print(list1)


#输出结果为:['love', 'peace', 'keep', 'beautiful', 'string']

3. Usage and examples of insert method

(1) Syntax: list.insert(object)

object: Inserting an object refers to inserting a value at a specified position in an existing list list. No result is returned, but the original list value will be modified, and the original list can be called to view it.

(2) Usage: You can insert the target value at the specified position in the original list.

(3) Examples

① Simple usage

#insert用法
list1 = ['love','peace','keep']
list1.insert(1,'wish')  #在1+1的位置插入‘wish’
list1

#输出结果:['love', 'wish', 'peace', 'keep']

②Use together with for function and dictionary dict

#insert用法
list1 = ['love','peace','keep']
dict1 = {'a':'wish','b':'happy','c':'keep on'}
i = 0
for value in dict1.values():
    i+=1 #从第2个位置开始逐个位置输入
    #print(i,value) #
    list1.insert(i,value)
list1
#['love', 'wish', 'happy', 'keep on', 'peace', 'keep']

Reference article:

Python List (List) | Rookie Tutorial (runoob.com)

The specific usage of the for function can refer to: Usage and examples of python's for loop statement

Guess you like

Origin blog.csdn.net/weixin_50853979/article/details/127441567