Python list Add new element, delete element, replace element

Source: Internet
Author: User

Python list Add new element

Now, there are 3 students in the class:

>>> L = [' Adam ', ' Lisa ', ' Bart ']

Today, the class transferred to a new classmate Paul, how to add new students to the existing list?

The first option is to append the append() new classmate to the end of the list by using the list method:

>>> L = [' Adam ', ' Lisa ', ' Bart ']>>> l.append (' Paul ') >>> print l[' Adam ', ' Lisa ', ' Bart ', ' Paul ' ]

append () always adds a new element to the tail of the list.

What if Paul says he is always on the test and asks to be added to the first place?

The method is to use the list insert() method, which accepts two parameters, the first parameter is the index number, and the second parameter is the new element to be added:

>>> L = [' Adam ', ' Lisa ', ' Bart ']>>> l.insert (0, ' Paul ') >>> print l[' Paul ', ' Adam ', ' Lisa ', ' Bar T ']

l.insert (0, ' Paul ') means that ' Paul ' will be added to the position of index 0 (i.e. the first one), while Adam, who originally indexed 0, and all the classmates behind it, automatically move backwards.

Suppose a new student paul,paul a classmate better than Bart, but worse than Lisa, he should be in the third place, in code:

L = [' Adam ', ' Lisa ', ' Bart ']l.insert (2, ' Paul ') print L
Python deletes elements from list

Paul's classmates had to turn away a few days ago, so how do we remove Paul from the existing list?

If Paul's classmates were in the last one, we could delete them using the list pop() method:

>>> L = [' Adam ', ' Lisa ', ' Bart ', ' Paul ']>>> L.pop () ' Paul ' >>> print l[' Adam ', ' Lisa ', ' Bart ']

The pop () method always deletes the last element of the list, and it returns the element, so we print out ' Paul ' after we execute L.pop ().

What if Paul's classmates aren't the last one? For example, Paul is ranked third:

>>> L = [' Adam ', ' Lisa ', ' Paul ', ' Bart ']

To kick Paul out of the list, we have to locate Paul's position first. Since Paul's index is 2, use Paul to erase it pop(2) :

>>> L.pop (2) ' Paul ' >>> print l[' Adam ', ' Lisa ', ' Bart '

Replacing elements in Python

Let's say the class is still 3 students:

>>> L = [' Adam ', ' Lisa ', ' Bart ']

Now, Bart's classmates are going to transfer, and happened to be a Paul classmate, to update the class membership list, we can first delete Bart, and then add Paul in.

Another way is to replace Bart with Paul directly:

>>> l[2] = ' paul ' >>> print LL = [' Adam ', ' Lisa ', ' Paul ']

To assign a value to an index in a list, you can replace the original element with the new element directly, and the list contains the same number of elements.

Since Bart can also index by-1, the following code can also do the same job:

>>> l[-1] = ' Paul '

The students in the class rank according to the score:

L = [' Adam ', ' Lisa ', ' Bart ']

However, after an exam, Bart's classmates accidentally made the first, and Adam classmate test the last.

Create a new rank by assigning a value to the index of the list.

L = [' Adam ', ' Lisa ', ' Bart ']
l[0]= ' Bart '
l[-1]= ' Adam '
Print L

Python list Add new element, delete element, replace element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.