Python list (Lists)

Source: Internet
Author: User
Tags python list

Python list (Lists)
Python list (Lists)

Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.

Python has six built-in sequence types, but the most common is list and metadata.

Operations that can be performed by sequences include indexing, slicing, adding, multiplication, and checking members.

In addition, Python has built-in methods to determine the sequence length and the maximum and minimum elements.

A list is the most common Python data type. It can appear as a comma-separated value in square brackets.

List data items do not need to have the same type

To create a list, you only need to enclose different data items separated by commas (,) in square brackets. As follows:

list1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5 ];list3 = ["a", "b", "c", "d"];

Like the string index, the List Index starts from 0. The list can be intercepted and combined.

Value in the access list

Use subscript indexes to access values in the list. You can also use square brackets to intercept characters, as shown below:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7 ];print "list1[0]: ", list1[0]print "list2[1:5]: ", list2[1:5]

Output result of the above instance:

list1[0]:  physicslist2[1:5]:  [2, 3, 4, 5]
To update the list, you can modify or update the data items in the list. You can also use the append () method to add the list items, as shown below:
#!/usr/bin/pythonlist = ['physics', 'chemistry', 1997, 2000];print "Value available at index 2 : "print list[2];list[2] = 2001;print "New value available at index 2 : "print list[2];

Note: we will discuss how to use the append () method in the following sections.

Output result of the above instance:

Value available at index 2 :1997New value available at index 2 :2001
Delete list elements

You can use the del statement to delete the list elements, as shown in the following example:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];print list1;del list1[2];print "After deleting value at index 2 : "print list1;

Output result of the above instance:

['physics', 'chemistry', 1997, 2000]After deleting value at index 2 :['physics', 'chemistry', 2000]

Note: we will discuss how to use the remove () method in the following sections.

Python list script Operator

The operators of list pairs + and * are similar to strings. + Is used to combine the list, and * is used to repeat the list.

As follows:

Python expressions Result Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
['Hi! '] * 4 ['Hi! ', 'Hi! ', 'Hi! ', 'Hi! '] Repeated
3 in [1, 2, 3] True Whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iteration
Python list Truncation

The list truncation and string operation types of Python are as follows:

L = ['spam', 'Spam', 'SPAM!']

Operation:

Python expressions Result Description
L [2] 'Spam! ' Reads the third element from the list.
L [-2] 'Spam' Reads the second-to-last element from the list.
L [1:] ['Spam', 'spam! '] Truncates the list from the second element.
Python list Functions & Methods

Python includes the following functions:

Serial number Function
1 Cmp (list1, list2)
Compare two list elements
2 Len (list)
Number of list elements
3 Max (list)
Returns the maximum value of the list element.
4 Min (list)
Returns the minimum value of the list element.
5 List (seq)
Convert tuples to a list

Python includes the following methods:

Serial number Method
1 List. append (obj)
Add a new object at the end of the list
2 List. count (obj)
Count the number of times an element appears in the list
3 List. extend (seq)
Append multiple values in another sequence at the end of the list (extend the original list with the new list)
4 List. index (obj)
Locate the index location of the first matching item of a value from the list
5 List. insert (index, obj)
Insert objects to the list
6 List. pop (obj = list [-1])
Removes an element from the list (the last element by default) and returns the value of this element.
7 List. remove (obj)
Removes the first matching item of a value from the list.
8 List. reverse ()
Elements in the reverse list
9 List. sort ([func])
Sort the original 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.