Python list: Modify, add, delete, and sort elements

Source: Internet
Author: User
Tags python list

This article refers to "Python programming: from getting started to practicing", Eric Matthes, translator: Ching

Operation Grammar Example Results
modifying elements

Motocycles = [' Honda ', ' Yamaha ', ' Suzuki ']

Print (Motocycles)

Motocycles[0] = ' Ducati '

Print (Motocycles)

[' Honda ', ' Yamaha ', ' Suzuki ']

[' Ducati ', ' Yamaha ', ' Suzuki ']

add element

 

Add an element at the end of the list: Append ()

 

motocycles = [' Honda ', ' Yamaha ', ' Suzuki ']

print (motocycles)

 

motocycles.append (' Ducati ')

print (motocycles)

 

[' Honda ', ' Yamaha ', ' Suzuki ']

 

[' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati ' ]

 

motocycles = [' Honda ', ' Yamaha ', ' Suzuki ']

print (motocycles)

 

motocycels.insert (0, ' Ducati ')

Print (motocycles)

 

[' Honda ', ' Yamaha ', ' Suzuki ']

 

[ ' Ducati ' , ' Honda ', ' Yamaha ', ' Suzuki ']

delete element

 

 

Delete by index: Del, unable to continue using

 

 

motocycles = [' Honda ', ' Yamaha ', ' Suzuki ']

Print (motocycles)

 

del motocycles[1]

print (motocycles)

 

 

[' Honda ', ' Yamaha ', ' Suzuki ']

 

[' Honda ', ' Suzuki ']

Delete the element at the end: Pop (), you can continue using

can also be deleted according to the index, such as Motocycles.pop (0)

 

Motocycles = [' Honda ', ' Yamaha ', ' Suzuki ']

Print (motocycles)

 

popped_motocycle = Motocycles.pop ()

print (motocycles)

print (popped_motocycle)

 

 

[' Honda ', ' Yamaha ', ' Suzuki ']

 

[' Honda ', ' Yamaha ']

 

suzuki

 

when you do not know the index, delete the element by value: Remove (), you can continue using

 

 motocycles = [' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati ']

Print (motocycles)

 

too_expensive = ' Ducati '

motocycles.remove (too_expensive)

print (motocycles)

print (too_expensive)

 [' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati ']

 

[' Honda ', ' Yamaha ', ' Suzuki ']

 

ducati

 

element sort

permanent sort: sort ()

 

Sort in reverse direction: sort (reverse = True)

Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

Print (Cars

 

cars.sort ()

Print (cars)

 

Span style= "COLOR: #000000" >cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

Cars.sort (reverse = True)

Print (cars)

[' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

 

[' Audi ', ' BMW ', ' Subaru ', ' Toyota ']

 

[' Toyota ', ' Subaru ', ' BMW ', ' Audi ']

temporary sort: sorted ()

 

Span style= "COLOR: #0000ff" > sort in reverse direction : sorted (reverse = True)

Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

Print (cars)

print (sorted (cars))

print (Cars)

[' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

 

[' Audi ', ' BMW ', ' Subaru ', ' Toyota ']

 

[' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

 

Permanently reverse the order of list elements: reverse ()

Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

Print (Cars)

Cars.reverse ()

Print (Cars)

Cars.reverse ()

Print (Cars)

[' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

[' Subaru ', ' Toyota ', ' Audi ', ' BMW ']

[' BMW ', ' Audi ', ' Toyota ', ' Subaru ']

Python list: Modify, add, delete, and sort elements

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.