current position:Home>Python adds list element append() method, extend() method and insert() method [details]

Python adds list element append() method, extend() method and insert() method [details]

2022-02-01 19:59:44 Parson old bird

A list is a data type that stores elements , Since you can store elements , So it's like a database , Some functions of addition, deletion, modification and query cannot be less . Let's take a look at it first Add list elements What are the methods .

One 、 Use + No. add list elements ****

In general, merging two lists is also a way to add elements , Just use + It's OK to operate directly , Here is the demo code .

name1 = ['python''java''php''MySql']
name2 = ['C++''C''C#']
total = name1 + name2
print(name1)
print(name2)
print(total)
 Copy code 

The operation results are as follows :

['python', 'java', 'php', 'MySql']\
['C++', 'C', 'C#']\
['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#']
 Copy code 

Two 、 Use append() Method to add a list element ****

append() Methods are available in many languages , This method belongs to append element , Append a single element or object or another list to the end of the list . All elements added 、 A list or object belongs to a single element in the list , It belongs to overall addition , Don't like + Add the list elements one by one like that .

1. Add a single element ****

name1 = ['python''java''php']
name1.append('MySql')
print(name1)

 Return results :['python', 'java', 'php', 'MySql']
 Copy code 

2. Add object ****

name1 = ['python''java''php']# name1.append('MySql')
name2 = ('MySql''SQL')
name1.append(name2)
print(name1)

 Return results :['python', 'java', 'php', ('MySql', 'SQL')]
 Copy code 

3. Add another list ****

name1 = ['python''java''php']
# name1.append('MySql')
# name2 = ('MySql', 'SQL')
name2 = ['C++''C''C#']
name1.append(name2)
print(name1)
 Copy code 

3、 ... and 、extend() Method add element ****

extend() and append() The difference is that :extend() You don't think of lists or Yuanzu as a whole , Instead, they add the elements they contain to the list one by one .

name1 = ['python''java''php']
name2 = ('MySql''SQL')
name1.extend(name2)
name3 = ['C++''C''C#']
name1.extend(name3)
print(name1)

 Return results :['python', 'java', 'php', 'MySql', 'SQL', 'C++', 'C', 'C#']
 Copy code 

Four 、insert() Method add element ****

The previous methods are to add elements at the end of the list ,insert() Method can add an element at the position specified in the list , Let's demonstrate .

name1 = ['python''java''php']
name1.insert(2'MySql')
print(name1)

 Return results :['python', 'java', 'MySql', 'php']
 Copy code 

In the above code, our index position is 2 Where to insert an element , The running results show that the index position seems to be different from what we think , This is because we misunderstood , there 2 Is to put the element to be inserted in the index position of 2 The place of .insert() Method can also insert other objects or lists , I'm not going to show you one by one .

copyright notice
author[Parson old bird],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011959422570.html

Random recommended