Data type of Python---list

Source: Internet
Author: User

Slice operation for List
List1 = [1,2,3,4,5,6,7,8,9]
Print (List1[0])
Print (list1[1])
Print (list1[3:])
Print (List1[-1])
Print (List1[-2])
Print (List1[::2])
12[4, 5, 6, 7, 8, 9]98[1, 3, 5, 7, 9]




List1=[‘Physics‘,‘Chemistry‘,1997,2000,[1,2,3]]
List2=[1, 2, 3, 4, 5 ]
List3 = ["a", "b", "C", "D" ]
You can insert any object in 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.

Update for list
List1=[]
list2=[1,2,3,4,5,6]
List1.append (' TT ')
List1.append (' hehe ') #append在list的最后添加
List1.insert (1, ' Test ') #在第二个元素下标位置添加一个test字符串, the element that originally moved in the first position moves backwards
List1.extend (List2) #在list1后面追加list2, List1 has changed, List2 has not changed.
List1.index (obj) #查找元素在list1的第几个位置
List1.pop (Index=-1) #将list1中的下表为-1 element Popup
List1.pop (2) #将下标为2的元素弹出
List1.reverse () #将list1进行翻转
List1.sort () #将list1进行升序排序



other ways to list
CMP (LIST1,LIST2) compares elements of two lists
Len (list1) View length of List1
Max (list1) View maximum value of List1
Min (list1) View minimum value of List1
A= ' Test '
Print (list (a)) -->[' t ', ' e ', ' s ', ' t '] list (seq) speak tuples/strings converted to list

A= (' Test ',)
Print (list (a))-->[' test ']

a=[1,2,3,2,5,2]
Print (A.count (2)) #3 statistics 2 How many times there are in a this list
list Delete Remove (element content) Pop (index) del List[i]
 It is worth mentioning that del is a Python expression, not a list method, so to call Del to delete the elements of the list, you need to delete through del List[index]. 
When you do not know the subscript location of the content, you can use remove for element deletion. If there are many identical elements in the element,


List1 = [ " aaa ", " BBCD ", " T ", 4,5,6,7,8,9" # List1.remove (' element name ') list1.remove (8" print (list1) #[' aaa ', ' bbcd ', ' t ', 4, 5, 6, 7, 9]

#list1. Pop () #默认弹出最后一个元素, or you can delete an element by using the specified index
List1.pop ()
Print (list1) #[' aaa ', ' bbcd ', ' t ', 4, 5, 6, 7, 8]

List1.pop (1)
Print (list1) #[' aaa ', ' t ', 4, 5, 6, 7, 8]



Del List[index]
List1 = [' aaa ', ' bbcd ', ' t ', 4,5,6,7,8,9]

Del List1[0]

Print (list1) #[' bbcd ', ' t ', 4, 5, 6, 7, 8, 9]

Data type of Python---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.