Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add list elements append (), extend () and insert () to python

Shulou Source: shulou.com Published: 2022-06-01 06:49:07 10月27日 Update

Xiaobian to share with you how to add list elements append(),extend() and insert() in python, I believe most people still do not know how to use, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

I. Use the + sign to add list elements

In general, the two lists are also combined as a method of adding elements, as long as the + sign is used to directly operate on it, the following is the demonstration code.

name1 = ['python',' java','php','MySql'] name2 = ['C++', ' C','C #'] total = name1 + name2 print(name1) print(name2) print(total) Run as follows: ['python',' java','php','MySql']['C++', ' C','C#']['python',' java','php','MySql', 'C++',' C#']

The append() method, found in many languages, is an appending method that appends a single element or object to the end of a list or another list. All elements, lists, or objects added are individual elements of the list, and are added as a whole, rather than adding list elements one by one like the + sign.

1. Add a single element name1 = ['python',' java','php'] name1.append ('MySql') print(name1)

Return Results:

['python', 'java', 'php', 'MySql']

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')]

3. Add another list name1 = ['python',' java','php']# name1.append ('MySql') # name2 =('MySql ', ' SQL') name2 = ['C++', 'C',' C#'] name1.append(name2) print(name1) III. extend() method Add element

The difference between extend() and append() is that extend() does not treat lists or ancestors as a whole, but adds 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#']

insert() method to add elements

The insert() method adds an element to the list at the specified position,

Here's a demonstration:

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

Return Results:

['python', 'java', 'MySql', 'php']

In the above code, we insert an element at index position 2. The result of running can be seen to be different from what we think the index position is. This is because we understand incorrectly that 2 here is to place the element to be inserted at index position 2. The insert() method can also insert other objects or lists

The above is "Python add list elements append(),extend() and insert() how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Element method result location object single article index different code content place whole demonstration run not much two most of the cases are in the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Shulou Information Apple Huawei