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
The sidebar is recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
guess what you like
-
Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
-
Python series -- web crawler
-
Plotly + pandas + sklearn: shoot the first shot of kaggle
-
How to learn Python systematically?
-
Analysis on several implementations of Python crawler data De duplication
-
leetcode 1616. Split Two Strings to Make Palindrome (python)
-
Python Matplotlib drawing violin diagram
-
Python crawls a large number of beautiful pictures with 10 lines of code
-
[tool] integrated use of firebase push function in Python project
-
How to use Python to statistically analyze access logs?
Random recommended
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis
- 1313. Unzip the coding list (Java / C / C + + / Python / go / trust)
- Python Office - Python edit word
- Collect it quickly so that you can use the 30 Python tips for taking off
- Strange Python strip
- Python crawler actual combat, pyecharts module, python realizes China Metro data visualization
- DOM breakpoint of Python crawler reverse
- Django admin custom field stores links in the database after uploading files to the cloud
- Who has powder? Just climb who! If he has too much powder, climb him! Python multi-threaded collection of 260000 + fan data
- Python Matplotlib drawing streamline diagram
- The game comprehensively "invades" life: Python releases the "cool run +" plan!
- Python crawler notes: use proxy to prevent local IP from being blocked
- Python batch PPT to picture, PDF to picture, word to picture script
- Advanced face detection: use Dlib, opencv and python to detect face markers
- "Python 3 web crawler development practice (Second Edition)" is finally here!!!!
- Python and Bloom filters
- Python - singleton pattern of software design pattern
- Lazy listening network, audio novel category data collection, multi-threaded fast mining cases, 23 of 120 Python crawlers
- Troubleshooting ideas and summary of Django connecting redis cluster
- Python interface automation test framework (tools) -- interface test tool requests
- Implementation of Morse cipher translator using Python program