1list.append()
2list.clear()
3list.copy()
4list.count()
5list.extend()
6list.index()
7list.insert()
8list.pop()
9list.remove()
10list.reverse()
11list.append()
12list.clear()
13list.copy()
14list.count()
15list.extend()
16list.index()
17list.insert()
18list.pop()
19list.remove()
20list.reverse()
21list.sort()
22sorted(list)
1my_list = ["banana", "cherry", "apple"]
2
3# len() : get the number of elements in a list
4print("Length:", len(my_list))
5
6# append() : adds an element to the end of the list
7my_list.append("orange")
8
9# insert() : adds an element at the specified position
10my_list.insert(1, "blueberry")
11print(my_list)
12
13# pop() : removes and returns the item at the given position, default is the last item
14item = my_list.pop()
15print("Popped item: ", item)
16
17# remove() : removes an item from the list
18my_list.remove("cherry") # Value error if not in the list
19print(my_list)
20
21# clear() : removes all items from the list
22my_list.clear()
23print(my_list)
24
25# reverse() : reverse the items
26my_list = ["banana", "cherry", "apple"]
27my_list.reverse()
28print('Reversed: ', my_list)
29
30# sort() : sort items in ascending order
31my_list.sort()
32print('Sorted: ', my_list)
33
34# use sorted() to get a new list, and leave the original unaffected.
35# sorted() works on any iterable type, not just lists
36my_list = ["banana", "cherry", "apple"]
37new_list = sorted(my_list)
38
39# create list with repeated elements
40list_with_zeros = [0] * 5
41print(list_with_zeros)
42
43# concatenation
44list_concat = list_with_zeros + my_list
45print(list_concat)
46
47# convert string to list
48string_to_list = list('Hello')
49print(string_to_list)
50
1>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
2>>> fruits.count('apple')
32
4>>> fruits.count('tangerine')
50
6>>> fruits.index('banana')
73
8>>> fruits.index('banana', 4) # Find next banana starting a position 4
96
10>>> fruits.reverse()
11>>> fruits
12['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
13>>> fruits.append('grape')
14>>> fruits
15['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
16>>> fruits.sort()
17>>> fruits
18['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
19>>> fruits.pop()
20'pear'
21
1list.append(x)
2#Add an item to the end of the list. Equivalent to a[len(a):] = [x].
3
4list.extend(iterable)
5#Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
6
7list.insert(i, x)
8#Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
9
10list.remove(x)
11#Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
12
13list.pop([i])
14#Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
15
1def all(iterable):
2 for element in iterable:
3 if not element:
4 return False
5 return True
6