Introduction to the Python list

Source: Internet
Author: User
Tags python list

What is a list: lists allow you to store groups of information in one place, which can contain several elements or millions of elements. The list is one of the lightest Python features that novices can use directly, and it incorporates the concepts of many important programming.
List definition: A list consists of a series of elements arranged in a particular order. Given that the list contains multiple elements, it is a good idea to specify a name for the list (such as letters, names). Use square brackets [] in Python to represent the list and separate the elements with commas.
For example:
[email protected] ~]# cat bicycles.py bicycles = [' Trek ', ' Cannondale ', ' redline ', ' specialized ']print (bicycles) [email Protected] ~]# python bicycles.py [' Trek ', ' Cannondale ', ' redline ', ' specialized ']
You can see that it's not what we want to see, and the list is printed, and Python will print the internal display of the list, including square brackets.

1.1.1 Accessing list elements
The list is an ordered collection, so to access any element of the list, simply tell Python the location or index of the element. To access a list element, the name of the expense list, and then the index of the element, is placed inside square brackets.
For example, extract the first element [[email protected] ~]# cat bicycles.py bicycles = [' Trek ', ' Cannondale ', ' redline ', ' specialized ']print ( Bicycles[0]) [[email protected] ~]# python bicycles.py trek
1.1.2 Index starting from 0 instead of 1
In Python, the index of the first list element is 0 instead of 1. This is true in most programming languages. This is related to the underlying implementation of the list operation.
To access any element of the list, you can reduce its position by 1 (n-1)
Python provides a special syntax for accessing the last list element. By specifying the index as 1 (-2 for the penultimate-3 for the third, and so on)
[[email protected] ~]# cat bicycles.py bicycles = [' Trek ', ' Cannondale ', ' redline ', ' specialized ']print (bicycles[-1]) [[ Email protected] ~]# Python bicycles.py specialized
The purpose is to access the last element without knowing the length of the list. This Convention also applies to other negative indexes.

1.1.3 using each value in the list
Extracts an element from the list and creates a message with this value.
[email protected] ~]# cat bicycles.py bicycles = [' Trek ', ' Cannondale ', ' redline ', ' specialized ']message = ' my first bicycl ' E was a "+ bicycles[0].title () +". " print (message) [[email protected] ~]# python bicycles.py My first bicycle was a Trek.
1.2 Modifying, adding, and deleting elements
1.2.1 Modifying list elements
To modify a list element, you specify the list name and the index of the element to modify, and then specify the new value of the element
[[Email protected] List]# cat modification.pymodification = [' Honda ', ' Yamaha ', ' Suzuki ']print (Modification) modification [0] = ' xiaoxin ' Print (modification) [[email protected] list]# python modification.py[' Honda ', ' Yamaha ', ' Suzuki ' [' Xiaoxin ', ' Yamaha ', ' Suzuki ']
1.2.2 Adding elements to a list
(1) Add an element at the end of the list
Method Append () Adds an element to the end of the list append: Added meaning
[Email protected] List]# cat modification.py modification = [' Honda ', ' Yamaha ', ' Suzuki ']print (Modification) modification [0] = ' xiaoxin ' Print (modification) modification.append (' Shuai ') print (modification) [[email protected] list]# python modification.py[' Honda ', ' Yamaha ', ' Suzuki ' [' Xiaoxin ', ' Yamaha ', ' Suzuki ' [' Xiaoxin ', ' Yamaha ', ' Suzuki ', ' Shuai ']
Method append dynamically Create a list--you can create an empty list and then add elements using a series of append () statements.
The advantage of this approach is that you often wait for the program to run before you know that the user wants to store that data in the program, and first create an empty list for controlling the user. Used to store values that the user will enter. Each of the differences provided by the user is then simply attached to the list.
[[Email protected] List]# cat modification.py message = []message.append (' Shuai ') message.append (' Xiao ') message.append (' Xin ') print (message) [[email protected] list]# python modification.py[' Shuai ', ' Xiao ', ' Xin ']
(2) Inserting elements into the list
Method Insert () adds space at index 0, and the element value is stored in this place. This action shifts each element in the list to the right one position
[[Email protected] List]# cat modification.py message = [' A ', ' B ', ' C ']message.insert (0, ' cat ') print (message) [email Protected] List]# python modification.py[' cat ', ' A ', ' B ', ' C ']
1.2.3 removing elements from the list
(1) Delete an element using the DEL statement
If you know the position of the element you want to delete in the list, you can use the DEL statement
[[Email protected] List]# cat modification.py message2 = [' 1 ', ' 2 ', ' 3 ']print (message2) del message2[2]print (Message2) [[ Email protected] List]# python modification.py[' 1 ', ' 2 ', ' 3 ' [' 1 ', ' 2 ']
Use Del to delete a list element at any location, as the condition is to know the index. With the DEL statement removed from the list, you will no longer be able to access it.
(2) pop () delete element
Sometimes you want to remove the element from the list and then use its value with Del obviously it's not going to work.
[[Email protected] List]# cat modification.py message3 = [' A ', ' B ', ' C ']print (message3) message4 = Message3.pop () print ( Message3) print (message4) [[email protected] list]# python modification.py[' A ', ' B ', ' C ' [' A ', ' B ']c
First, define a list and print, then store the deleted elements in another variable, and finally print the deleted elements to delete the elements by the root index. For example message4 = Message3.pop (2)
1.2.4 deleting elements based on values
Sometimes you don't know where to remove the value from the list, and if you only know the value of the element you want to delete, you can use remove ()
[[Email protected] list]# python modification.py [' A ', ' B ', ' C ', ' D '] [' a ', ' C ', ' D '][[email protected] list]# cat modification . py Message5 = [' A ', ' B ', ' C ', ' D ']print (message5) message5.remove (' B ') print (MESSAGE5)

1.3 Organization List
1.3.1 using sort () to sort a list permanently
In the Create list, it is often necessary to sort the elements. Use sort () to sort the list permanently.
[[Email protected] List]# cat cars.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']cars.sort () print (cars) [[email protected] list] # python cars.py [' Audi ', ' BMW ', ' Subaru ', ' Toyota ']
You can also simply pass parameters to the sort () method in the reverse order of the alphabetical order. Reverse=true, note that the ' T ' in True must capitalize
[Email protected] List]# cat cars.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']cars.sort (reverse=true) print (cars) [email Protected] List]# python cars.py [' Toyota ', ' Subaru ', ' BMW ', ' Audi ']
The first two methods modify the order of the list to be permanent.

1.3.2 Use function sorted () to sort a list temporarily
[Email protected] List]# cat cars1.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']print (sorted (cars)) [[Email protected] list]# Python cars1.py [' Audi ', ' BMW ', ' Subaru ', ' Toyota ']
Warm tips:
On the order of the modification of permanent and temporary can be so understood, when the sorting after the permanent effect is the next time the list is printed, the elements are arranged in order, two temporary two times when printing, the order is canceled. For example: [[email protected] List]# cat cars.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']cars.sort () print (cars) print (cars) [[ Email protected] List]# python cars.py [' Audi ', ' BMW ', ' Subaru ', ' Toyota ' [' Audi ', ' BMW ', ' Subaru ', ' Toyota '] temp sort: [[Email PR Otected] List]# cat cars1.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']print (sorted (cars)) print (cars) [[email protected] list] # python cars1.py [' Audi ', ' BMW ', ' Subaru ', ' Toyota ' [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']
1.3.3 The Print List backwards
The so-called inverted print, is reversed printing, not alphabetical sort of printing. Permanently modifies the order in which the list elements are arranged, but may revert to the original order at any time, just call reverse () again.
[Email protected] List]# cat cars.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']cars.reverse () print (cars) [[email protected] List]# python cars.py [' Subaru ', ' Toyota ', ' Audi ', ' BMW ']
1.3.4 determines the length of the list, Python calculates the list element starting from 1
[Email protected] List]# cat cars.py cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']print (len (Cars)) [[Email protected] list]# Python cars.py 4

Introduction to the Python 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.