7 Python Data Type-list

Source: Internet
Author: User
Tags python list

List is one of the most commonly used data structures in Python and other languages. Python uses brackets [] to parse the 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.

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.

 1  list1 = [ " physics   ", "  chemistry   ", 1997, 2000];  2  list2 = [1, 2, 3, 4, 5];  3  list3 = [ "  A  , "   b  , "   C  , "   D  ]; 
View Code
1Li = [1,'a','b', 2, 3,'a']2 #Li.insert (0,55) #按照索引去增加3 #Print (LI)4 #5 #li.append (' aaa ') #增加到最后6 #Li.append ([i]) #增加到最后7 #Print (LI)8 #9 #li.extend ([' q,a,w ']) #迭代的去增Ten #li.extend ([' q,a,w ', ' aaa ']) One #li.extend (' a ') A #li.extend (' abc ') - #li.extend (' a,b,c ') - #Print (LI)
the list increases
1 #L1 = li.pop (1) #按照位置去删除, with return value2 #print (L1)3 4 #del Li[1:3] #按照位置去删除, can also be sliced to delete no return value. 5 #Print (LI)6 7 #li.remove (' a ') #按照元素去删除8 #Print (LI)9 Ten #li.clear () #清空列表
the deletion of the list
1 # Li = [1, ' A ', ' B ', 2,3, ' a '] 2 # li[1] = ' Dfasdfas ' 3 # Print (LI) 4 # Li[1:3] = [' A ', ' B '] 5 # Print (LI)
changes to the list
1 #slice it, or cycle it. 2List1 = ['Physics','Chemistry', 1997, 2000];3List2 = [1, 2, 3, 4, 5, 6, 7 ];4 5 Print "List1[0]:", List1[0]6 Print "List2[1:5]:", List2[1:5]
List of searchPython 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.

Python Expressions Results 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 Iteration
Python list interception
Python Expressions Results Description
L[2] ' Taobao ' Read the third element in the list
L[-2] ' Runoob ' Reads the second-to-last element in a list
L[1:] [' Runoob ', ' Taobao '] To intercept a list starting with the second element
Python list Functions & methods
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

7 Python Data Type-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.