Python lists (list)

Source: Internet
Author: User
Tags python list

Pythonlisting (list)

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

Python has 6 built-in types of sequences, but the most common are lists and tuples.

Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.

Data items for a list do not need to have the same type

Create a list by enclosing separate data items separated by commas in square brackets. As shown below:

List1 = ['physics'chemistry', 1997, += [1, 2, 3, 4, 5= ["a""b""C"  "D"];

As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.

Accessing values in a list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

# !/usr/bin/python  = ['physics'chemistry', 1997, += [1, 2, 3, 4, 5, 6, 7 ]; Print "  ", list1[0]print"", List2[1:5]

The result of the above example output:

list1[0]:  physicslist2[1:5]:  [2, 3, 4, 5]
Update list

You can modify or update the list's data items, or you can use the Append () method to add the list items as follows:

#!/usr/bin/pythonList= ['Physics','Chemistry', 1997, 2000];Print "Value available at index 2:"PrintList[2];list[2] = 2001;Print "New value available at index 2:"PrintLIST[2];

Note: We will discuss the use of the Append () method in the following chapters

The result of the above example output:

Value available at index 2 :19972 :2001
To delete a list element

You can use the DEL statement to delete the elements of a list, as in the following example:

# !/usr/bin/python  = ['physics'chemistry', 1997, +]; Print List1; del list1[2]; Print "  "print list1;

The result of the above example output:

['physics'chemistry', 1997,2 : [  'physics'chemistry', 2000]

Note: We will discuss the use of the Remove () method in the following chapters

Python List script operators

The operands of the list to + and * are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists.

As shown below:

Python expression 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! '] Repeat
3 in [1, 2, 3] True whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 iterations

Python list interception

A list of Python instances is captured as follows:

>>> L = ['Google','Runoob','Taobao']>>> l[2]'Taobao'>>> l[-2]'Runoob'>>> l[1:] ['Runoob','Taobao']>>>

Describe:

Python Expression Result Description
L[2] ' Taobao ' reads the third element of the list
L[-2] ' Runoob ' reads the second-to-last element in a list
L[1:] [' Runoob ', ' Taobao '] starting with the second element to intercept a list

Python list Functions & methods

Python contains the following functions:

Serial Number function
1 CMP (List1, List2)
Compare two elements of a list
2 Len (list)
Number of list elements
3 Max (list)
Returns the maximum value of a list element
4 MIN (list)
Returns the minimum value of a list element
5 List (seq)
Convert a tuple to a list

Python contains 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 occurrences of an element in a list
3 List.extend (seq)
Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4 List.index (obj)
Find the index position of the first occurrence of a value from the list
5 List.insert (index, obj)
Inserting an object into a list
6 List.pop (Obj=list[-1])
Removes an element from the list (the last element by default), and returns the value of the element
7 List.remove (obj)
To remove the first occurrence of a value in a list
8 List.reverse ()
Reverse List of elements
9 List.sort ([func])
Sort the original list
List of Notes

Python creates a two-dimensional list that writes the required parameters to cols and rows

 for inch  for  in range (rows)]

Instance:

 for inch  for  in range (5)]>>> list_2d[0].append (3)>>> list_2d[0].append (5) >>> list_2d[2].append (7)>>>3, 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
#!/usr/bin/python#-*-coding:utf-8-*-list01= ['Runoob', 786, 2.23,'John', 70.2]list02= [123,'John']Printlist01Printlist02#List InterceptionPrintList01[0]PrintList01[-1]PrintList01[0:3]#Duplicate listPrintLIST01 * 2#list CombinationsPrintLIST01 +list02#Get list lengthPrintLen (list01)#To delete a list elementdelList02[0]Printlist02#whether the element exists in the listPrint 'John' inchlist02#True#Iteration forIinchlist01:PrintI#compare two elements of a listPrintCMP (list01, list02)#list Maximum/minimum valuePrintMax ([0, 1, 2, 3, 4])PrintMin ([0, 1])#convert a tuple to a listAtuple= (1,2,3,4) list03=list (atuple)Printlist03#add a new element at the end of the listList03.append (5)Printlist03#append multiple values from another sequence at the end of the list (extend the original list with a new list)list03.extend (list01)Printlist03#Count the number of occurrences of an element in a listPrintList03.count (1)#find the index position of the first occurrence of a value from the listPrintList03.index ('John')#inserting an object into a listlist03.insert (0,'Hello')Printlist03#removes an element from the list (the last element by default), and returns the value of the elementPrintlist03.pop (0)Printlist03#to remove the first occurrence of a value in a listList03.remove (1)Printlist03#reverse List of elementsList03.reverse ()Printlist03#sort the original listList03.sort ()Printlist03
>>> list4=[123,["das","AAA"],234]>>>List4>>>"AAA" inchList4#in can only judge one level of elementFalse>>>"AAA" inchLIST4[1]#Select a list in the list to determineTrue>>> list4[1][1]'AAA'

Python lists (list)

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.