About Python learning notes 2_ list

Source: Internet
Author: User

1.1 What is a list?

In Python, the list is represented by square brackets ([]), and the elements are separated by commas.

The list is an ordered collection, so to access any element of the list, simply tell Python the location or index of the element. Python provides a special syntax for accessing the last list element. By specifying the index as 1, you can have Python return the last list element:

Bicycles = ['trek''Cannondale'redline ' ' Specialized ' ]print (bicycles)print(bicycles[0])print( BICYCLES[-1])

1.2 modifying, adding, and deleting elements1. Modifications
Motorcycles = ['Honda'Yamaha'Suzuki ' ]print'Ducati'print(motorcycles)

2. Adding elements to the list

1) add element at the end of the list, method append () Adds the element ' Ducati ' to the end of the list

Motorcycles = ['Honda','Yamaha','Suzuki']Print(motorcycles) motorcycles.append ('Ducati')Print(motorcycles) Motorcycles=[]motorcycles.append ('Honda') Motorcycles.append ('Yamaha') Motorcycles.append ('Suzuki')Print(motorcycles)

2) Insert an element in the list, use the method Insert () to add a new element anywhere in the list

Motorcycles = ['Honda'Yamaha'Suzuki '  'Ducati')print(motorcycles)

3) Remove the element from the list,

Use the DEL statement to delete the element and, if you know the position of the element you want to delete in the list, use the DEL statement

Motorcycles = ['Honda'Yamaha'Suzuki ' ]print(motorcycles)del  motorcycles[0]print( Motorcycles)

Using the method Pop () to delete an element, the method pop () removes the element at the end of the list and allows you to continue to use it. The term pop-up is derived from the analogy that a list is like a stack, and deleting the element at the end of the list is equivalent to ejecting the top element of the stack. In fact, you can use POP () to delete an element from anywhere in the list, just specify the index of the element you want to delete in parentheses.

Motorcycles = ['Honda'Yamaha'Suzuki ' ]print= motorcycles.pop ()print(motorcycles) Print (popped_motorcycle)

            deleting elements based on values, sometimes not knowing where to remove the values from the list. If you only know the value of the element you want to delete, you can use the method remove (). The Remove () method removes only the first specified value. such as                 The value you want to delete may appear more than once in the list, and you need to use a loop to determine whether all such values have been deleted.

Motorcycles = ['Honda'Yamaha'Suzuki ' ' Ducati ' ]print(motorcycles) motorcycles.remove ('Ducati')  )print(motorcycles)

1.3 Organization List

In the list you create, the order of the elements is often unpredictable, because you do not always control the order in which users provide the data.

1. How to use sort () to sort a list in a permanent order

Suppose you have a list of cars and want to make the cars in alphabetical order. The method sort () permanently modifies the order in which the list elements are arranged and cannot be restored to the original order. You can also arrange list elements in the reverse order of alphabetical order, and simply pass the parameter reverse=true to the sort () method.

Cars = ['bmw'Audi'Toyota ' 'Subaru']cars.sort ()print(cars) cars.sort (Reverse= True) Print (Cars)

2. Use the function sorted () to sort the list temporarily

To preserve the original order of the list elements and render them in a specific order, you can use the function sorted ().

Cars = ['BMW','Audi','Toyota','Subaru']Print("Here is the original list:")Print(Cars)Print("\nhere is the sorted list:")Print(sorted (cars))Print("\nhere is the original list again:")Print(Cars)

3. Reverse the Print List

To reverse the order of the list elements, you can use the method reverse (). Method reverse () permanently modifies the order in which the list elements are arranged, but can revert to the original order at any time, just call reverse () again on the list.

Cars = ['bmw'Audi'Toyota ' 'Subaru']print(cars) cars.reverse ()print (Cars)

4. Determine the length of the list

Use the function Len () to quickly learn the length of a list

Cars = ['bmw'Audi'Toyota ' 'Subaru']a=len (cars)print(a)

About Python learning notes 2_ list

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.