Python-a04-List,Tuple,Set

This article shows the examples of List, Tuple, Set in python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
## List 
# List basics
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses)
print(len(courses))
print(courses[0])
print(courses[-1]) # start from the end
print(courses[4]) # error
print(courses[0:2]) # the first index include, not the second one
print(courses[:2]) # from the first to index 2
print(courses[2:]) # from index 2 to the end

# Add elements to List
courses1 = ['History', 'Math', 'Physics', 'CompSci']
courses1.append('Art')
print(courses1) # ['History', 'Math', 'Physics', 'CompSci', 'Art']

courses2 = ['History', 'Math', 'Physics', 'CompSci']
courses2.insert(0, 'Art')
print(courses2) # ['Art', 'History', 'Math', 'Physics', 'CompSci']


courses3 = ['History', 'Math', 'Physics', 'CompSci']
courses4 = ['Art', 'Education']
courses3.insert(0, courses4)
print(courses3) # [['Art', 'Education'], 'History', 'Math', 'Physics', 'CompSci']

courses5 = ['History', 'Math', 'Physics', 'CompSci']
courses6 = ['Art', 'Education']
courses5.extend(courses6)
print(courses5) # ['History', 'Math', 'Physics', 'CompSci', 'Art', 'Education']

# Remove elements to List
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.remove('Math')
print(courses) #['History', 'Physics', 'CompSci']

courses1 = ['History', 'Math', 'Physics', 'CompSci']
courses.pop()
print(courses) #['History', 'Math', 'Physics']


courses2 = ['History', 'Math', 'Physics', 'CompSci']
popped = courses.pop()
print(popped) #'CompSci'

# Order in List
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.reverse()
print(courses) #['CompSci', 'Physics', 'Math', 'History']

courses1 = ['History', 'Math', 'Physics', 'CompSci']
courses1.sort()
print(courses1) #['CompSci', 'History', 'Math', 'Physics']

courses2 = ['History', 'Math', 'Physics', 'CompSci']
courses2.sort(reverse=True)
print(courses2) #['Physics', 'Math', 'History', 'CompSci']


courses3 = ['History', 'Math', 'Physics', 'CompSci']
courses4 = sorted(courses3)
print(courses4) #['CompSci', 'History', 'Math', 'Physics']

# Min, Max, Sum in List
nums = [1,5,2,4,3]
print(min(nums)) #1
print(max(nums)) #5
print(sum(nums)) #15

# index in List
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses.index('ComSci')) # 3
print(courses.index('Art')) # error
print('Art' in courses) # False
print('Math' in courses) # True

for item in courses:
print(item) # History Math Physics CompSci

for index, course in enumerate(courses):
print(index, course) # 0 History 1 Math 2 Physics 3 CompSci

for index, course in enumerate(courses, start=1):
print(index, course) # 1 History 2 Math 3 Physics 4 CompSci

# join in List
courses = ['History', 'Math', 'Physics', 'CompSci']
course_str = ', '.join(courses)
new_courses = course_str.split(', ')
print(course_str) # 'History', 'Math', 'Physics', 'CompSci'
print(new_courses) # ['History', 'Math', 'Physics', 'CompSci']

## Tuple
# List is mutable, Tuple is immnutable, we can't add, append, modify element in Tuple
list1 = ['History', 'Math', 'Physics', 'CompSci']
list2 = list1
print(list1) #['History', 'Math', 'Physics', 'CompSci']
print(list2) #['History', 'Math', 'Physics', 'CompSci']

list1[0] = 'Art'
print(list1) #['Art', 'Math', 'Physics', 'CompSci']
print(list2) #['Art', 'Math', 'Physics', 'CompSci']

tuple1 = ('History', 'Math', 'Physics', 'CompSci')
tuple2 = tuple1
print(tuple1) #('History', 'Math', 'Physics', 'CompSci')
print(tuple2) #('History', 'Math', 'Physics', 'CompSci')

tuple1[0] = 'Art' # error


## Set
# Set values are unordered and unduplicated
set1 = {'History', 'Math', 'Physics', 'CompSci'}
print(set1) #{'Math', 'History', 'Physics', 'CompSci'} order may change

set2 = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print(set2) #{'Math', 'History', 'Physics', 'CompSci'} order may change

set3 = {'History', 'Math', 'Physics', 'CompSci'}
print('Math' in set3) #True Set is optimized for the check existing

# Set is optimized to find the same and differences between 2 ones
set4 = {'History', 'Math', 'Physics', 'CompSci'}
set5 = {'History', 'Math', 'Art', 'Design'}
print(set4.intersaction(set5)) #{'History', 'Math'}
print(set4.difference(set5)) #{'Physics', 'CompSci'}
print(set4.union(set5)) #{'History', 'Math', 'Physics', 'CompSci', 'Art', 'Design'}

## How to create empty List, Tuple, Set
empty_list = []
empty_list = list()

empty_tuple = ()
empty_tuple = tuple()

empty_set = {} # This is wrong, it's a dict
empty_set = set()