1# list() converts other datatypes into lists
2
3# e.g.
4
5text = 'Python'
6
7# convert string to list
8text_list = list(text)
9print(text_list)
10
11# check type of text_list
12print(type(text_list))
13
14# Output: ['P', 'y', 't', 'h', 'o', 'n']
15# <class 'list'>
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# empty list
2my_list = []
3
4# list of integers
5my_list = [1, 2, 3]
6
7# list with mixed data types
8my_list = [1, "Hello", 3.4]
1list = [1, 2, 3, 4, 5, 6, 7, 9, 10]
2
3print("Original list", list)
4
5list[4] = 99
6
7print("Updated 1", list)
8
9# del is to delete
10
11del list[4]
12
13print("Updated 2", list)
14
15list.append(12)
16
17print("Updated 3", list)
18
19list.remove(3)
20
21print("Updated 4", list)
22
23list.pop()
24
25print("Updated 5", list)
26
27list.insert(0, 9)
28# first index is the index value where u wanna put and second is what the value is
29
30print("Updated 6", list)
31
32list.insert(0, 99)
33
34print("Updated 7", list)
35
36# How to add a list into another list
37
38second_list = [8, 9, 10]
39list[3] = second_list
40print("List after adding a new list into an exiting list", list)
41
42# How to find the mode
43
44print("Counting the number of appearances", list.count(9))
45
46
47lis = [1, 4, 6, 1, 5]
48print("Number 1 is in", lis.index(1))
49
50print("Get lenght of the list", len(list))
51
52list1 = [1, 2, 3, 4, 5, 6, 7]
53list2 = [8, 9, 10, 11, 12, 13, 14]
54
55print("Join two lists ", list1 + list2)
56
57print("Repeat the element in the list", "\nHello Sneh\n" *10)
58
59