Python List base operations

Source: Internet
Author: User

Python list basic operations

Remember a word, called Gu head regardless of the tail

Let's start by defining a list

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]

Next, we'll do a series of operations on this list.

1. Slicing

Take the first and second of the values

Print (name[0],name[1]) results Jixuege Dajiba

Description

The above 0 and 1 refer to the small mark, from left to right in order from 0 onwards to 4

Print an interval

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]print (Name[:2]) print results [' jixuege ', ' Dajiba ']
Print the last one
name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]print (Name[-1]) print results Youtianai
2. Append
For example, I want to add a person in.
name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.append ("Xiha") print (name) results [' Jixuege ', ' Xitong ', ' Dajiba ', ' Youhua ', ' Boduoye ', ' cangjinkong ', ' Youtianai ', ' Xiha '

This will be appended to the end.

Another is inserting insert

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.insert (2, "Youhua") print (name) results [' Jixuege ', ' Dajiba ', ' Youhua ', ' Boduoye ', ' cangjinkong ', ' Youtianai ']

3. Modification

Direct modification can

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name[2]= "wuming" Print (name) results [' Jixuege ', ' Dajiba ', ' wuming ', ' cangjinkong ', ' Youtianai ']

4. Delete

Direct emptying

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.clear () print (name) results []
Delete the specified element
name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.remove ("Jixuege") print (name) results [' Dajiba ', ' Boduoye ', ' cangjinkong ', ' Youtianai ']

Delete the specified subscript

name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]del name[2]print (name) print results [' jixuege ', ' Dajiba ', ' Cangjinkong ', ' Youtianai ']
Do not specify subscript to delete the last
name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.pop () print (name) results [' Jixuege ', ' Dajiba ', ' Boduoye ', ' Cangjinkong ']
Specify subscript
name = ["Jixuege", "Dajiba", "Boduoye", "Cangjinkong", "Youtianai"]name.pop (1) print (name) results [' Jixuege ', ' Boduoye ', ' Cangjinkong ', ' Youtianai ']

Python List base operations

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.