首页 > 代码库 > Python List

Python List

1. Basic

list1 = [‘math‘, 1, 2.3]

  

2. Accessing

list1 = [‘math‘, 1, 2.3]
list1[0]
list1[1:2]

  

3. delete

list1 = [‘math‘, 1, 2.3]
del list1[0] #‘math‘ will be deleted

  

4. Built-in function

cmp(list1, list2) #Compares elements of both lists.

len(list) #Gives the total length of the list.

max(list) #Returns item from the list with max value.

min(list)

list(seq) #Converts a tuple into list.

list.append(obj) #Appends object obj to list

list.count(obj) #Returns count of how many times obj occurs in list

list.extend(seq) #Appends the contents of seq to list

list.index(obj) #Returns the lowest index in list that obj appears

list.insert(index, obj) #Inserts object obj into list at offset index

list.pop(obj=list[-1]) #Removes and returns last object or obj from list

list.remove(obj) #Removes object obj from list

list.reverse() #Reverses objects of list in place

list.sort([func]) #Sorts objects of list, use compare func if given

  

Python List