1 list = ['larry', 'curly', 'moe']
2 list.append('shemp') ## append elem at end
3 list.insert(0, 'xxx') ## insert elem at index 0
4 list.extend(['yyy', 'zzz']) ## add list of elems at end
5 print list ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
6 print list.index('curly') ## 2
7
8 list.remove('curly') ## search and remove that element
9 list.pop(1) ## removes and returns 'larry'
10 print list ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
1# Basic syntax:
2your_list.append('element_to_append')
3
4# Example usage:
5your_list = ['a', 'b']
6your_list.append('c')
7print(your_list)
8--> ['a', 'b', 'c']
9
10# Note, .append() changes the list directly and doesn’t require an
11# assignment operation. In fact, the following would produce an error:
12your_list = your_list.append('c')
1 list = [] ## Start as the empty list
2 list.append('a') ## Use append() to add elements
3 list.append('b')
1 list = ['a', 'b', 'c', 'd']
2 print list[1:-1] ## ['b', 'c']
3 list[0:2] = 'z' ## replace ['a', 'b'] with ['z']
4 print list ## ['z', 'c', 'd']
1 list = [1, 2, 3]
2 print list.append(4) ## NO, does not work, append() returns None
3 ## Correct pattern:
4 list.append(4)
5 print list ## [1, 2, 3, 4]