Operation |
List |
Method |
Example |
Increase |
List.append (obj) add element to end |
eg. >>> list1=[' hello ', ' world ', ' How ', ' is ', ' you ' >>> list1.append ('! ') >>> List1 [' Hello ', ' world ', ' How ', ' is ', ' you ', '! '] |
List.insert (index, obj) add element to specified position Index: Indexed location Obj: Content |
eg. >>> List1 [' Hello ', ' world ', ' How ', ' is ', ' you ', '! '] >>> List1.insert (1, ', ') >>> List1 [' Hello ', ', ', ' world ', ' How ', ' is ', ' you ', '! '] |
List.extend (list_i) Adds an element from the List_i list to the list |
eg. >>> List [' Hello ', ' how ', ' is ', ' you '] >>> list.extend ([' Good ', ' girl ') >>> List [' Hello ', ' how ', ' is ', ' you ', ' good ', ' girl '] |
Delete |
List.pop (): Removes the element at the end of the list by default List.pop (Index) Deletes the element at the specified position, index |
eg. >>> List1 [' Hello ', ', ', ' world ', ' How ', ' is ', ' you ', '! '] >>> List1.pop () ‘!‘ >>> List1.pop (1) ‘,‘ |
Del List[index] Deletes the element at the specified position, index Del List Delete entire list |
eg. >>> List1 [' Hello ', ' world ', ' How ', ' is ', ' you '] >>> del List1[1] >>> List1 [' Hello ', ' how ', ' is ', ' you ']
>>> List1 [' Hello ', ' how ', ' is ', ' you '] >>> del List1 >>> List1 Traceback (most recent): File "<stdin>", line 1, in <module> Nameerror:name ' List1 ' is not defined |
List.remove (obj) Remove the first element of the list equal to obj |
eg. >>> list=[' hello ', ' world ', ' How ', ' is ', ' you ' >>> List.remove (' World ') >>> List [' Hello ', ' how ', ' is ', ' you '] |
List.clear () Clear all the contents of the list |
eg. >>> list=[' hello ', ' world ', ' How ', ' is ', ' you ' >>> List.clear () >>> List [] |
Modify |
List[index]=obj Modifies the element at the specified position |
eg. >>> List1 [' Hello ', ' world ', ' How ', ' is ', ' you '] >>> list1[0]= ' Hi ' >>> List1 [' Hi ', ' world ', ' How ', ' is ', ' you '] |
Inquire |
List[index] Indexed by subscript, starting from 0 |
eg. >>> list=[' hello ', ' world ', ' How ', ' is ', ' you ' >>> List[2] ' How ' |
LIST[A:B] Slices, Gu Tou regardless of the tail |
eg. >>> list=[' hello ', ' world ', ' How ', ' is ', ' you ' >>> List[0:3] [' Hello ', ' world ', ' how '] >>> list[1:] [' World ', ' How ', ' is ', ' you '] >>> List[:3] [' Hello ', ' world ', ' how '] >>> list[:] [' Hello ', ' world ', ' How ', ' is ', ' you '] |