程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Addition and deletion of Python list basic operations

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Basic operations for lists , About adding and deleting syntax .


Two 、 Add elements

1、append() Method

Use append() Method to add a new element to the end of the list , The format is as follows .

  • list Represents a list
  • obj Represents an object added to the end of the list

list.append(obj)

for example :

add_list = [0, 1, 2, 3] # Create a list of add_list And the assignment
add_list.append(4) # Use append() Methods add_list Add element at end 4
print(add_list)

give the result as follows .


2、extend() Method

Use extend() Method can append multiple values in another sequence at the end of the list at one time , The format is as follows .

  • list Represents a list
  • seq Represents the list of elements added to the list

list.extend(seq)

example : Compare extend() Methods and append() Method to add list elements .

list_1 = [1, 2, 3] # Create a list of list_1 And the assignment
list_2 = [4, 5, 6] # Create a list of list_2 And the assignment
list_3 = [7, 8, 9] # Create a list of list_3 And the assignment
list_1.append(list_2) # take list_2 As an object , The whole package is added to list_1 In the object
print(list_1) # Output list_1
list_2.extend(list_3) # take list_3 As a sequence , Combine this sequence with list_2 Sequence merging
print(list_2) # Output list_2

give the result as follows .


3、 ... and 、 Remove elements

1、del command

del command : The elements in the list can be deleted according to the index , You can also use fragmentation to delete elements in the list ..

  • list Represents a list
  • obj Represents the object to find

list.index(obj)

example : Find elements in the list , If you find , Output the index position of the element in the list , Otherwise, the output is not found .

number = [1,2,3,4,5] # Create a list of number And the assignment
del number[2] # Use del Command deletion number The index for 2 The elements of
print(number)
number = [1,2,3,4,5] # Create a list of number And the assignment
del number[1:3] # Use del Delete index from command 1 To 3( barring 3) The elements of
print(number)

give the result as follows .

2、pop() Method

Used to remove an element from the list ( Default to last element ), And returns the value of that element , The format is as follows .

  • list Represents a list
  • obj Is an optional parameter , Indicates the index value of the removed list element , The default is -1, Delete the last list value

list.pop([obj])

for example :

number = [1, 2, 3, 4, 5] # Create a list of number And the assignment
print(number.pop()) # Use pop() Methods to remove number The elements in
print(number)
print(number.pop(0)) # Use pop() Methods to remove number The index for 0 The elements of
print(number) # Print number

give the result as follows .

3、remove() Method

Used to remove the first match of a value in the list , The format is as follows .

  • list Represents a list
  • obj Represents the object to be removed from the list

list.remove(obj)

example : Delete all specified elements in the list .

x = ['123', 'abc', 'xyz', 'abc', 'python'] # Create a list of x And the assignment
while 'abc' in x: # loop
x.remove('abc') # Use remove() Methods to remove x The median is 'abc' The elements of
print(x) # Output x

give the result as follows .


Four 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


5、 ... and 、 summary

The above is about Python Basic operations for lists , About adding and deleting syntax , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved