1#Creating lists
2my_list = ['foo', 4, 5, 'bar', 0.4]
3my_nested_list = ['foobar', ['baz', 'qux'], [0]]
4
5#Accessing list values
6my_list[2] # 5
7my_list[-1] # 0.4
8my_list[:2] # ['foo', 4, 5]
9my_nested_list[2] # ['baz', 'quz']
10my_nested_list[-1] # [0]
11my_nested_list[1][1] # 'qux'
12
13#Modifying lists
14my_list.append(x) # append x to end of list
15my_list.extend(iterable) # append all elements of iterable to list
16my_list.insert(i, x) # insert x at index i
17my_list.remove(x) # remove first occurance of x from list
18my_list.pop([i]) # pop element at index i (defaults to end of list)
19my_list.clear() # delete all elements from the list
20my_list.index(x[, start[, end]]) # return index of element x
21my_list.count(x) # return number of occurances of x in list
22my_list.reverse() # reverse elements of list in-place (no return)
23my_list.sort(key=None, reverse=False) # sort list in-place
24my_list.copy() # return a shallow copy of the list
1# example of list in python
2
3myList = [9, 'hello', 2, 'python']
4
5print(myList[0]) # output --> 9
6print(myList[-3]) # output --> hello
7print(myList[:3]) # output --> [9, 'hello', 2]
8print(myList) # output --> [9, 'hello', 2, 'python']
9
1#Creating lists
2my_list = [1, "Hello", 3.4, 0, "World"]
3my_nested_list = [['Hello', 'World'],[47,39]]
4
5#Accessing lists
6my_list[1] # Hello
7my_list[-2] # 0
8my_list[:3] # [1, "Hello", 3.4]
9my_nested_list[1] #[47,39]
10my_nested_list[0][1] # World
1# Create list
2List = list()
3List = []
4List = [0, "any data type can be added to list", 25.12,
5 ("Even tuples"), {"Dictionaries": "can also be added"},
6 ["can", "be nested"]]
7# Accessing items
8
9List[1] # 0
10List[-1] # ["can", "be nested"]
11
12# Operations
13List.append(4) # adds 4 to end
14List.pop(n=-1) # removes element from nth position
15List.count(25.12) # 1
1a1 = [1, 'x', 'y']
2a2 = [1, 'x', 'y']
3a3 = [1, 'x', 'y']
4
5b = [2, 3]
6
7a1.append(b)
8a2.insert(3, b)
9a3.extend(b)
10
11print(a1)
12print(a2)
13print(a3