Python List Replacement Element

Source: Internet
Author: User
Tags python list
Replace direct corresponding position assignment 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 '

L[-1] The same effect as L[-1+len (L)]

Python List Replacement Element

Related Article

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.