-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlists.py
More file actions
102 lines (84 loc) · 1.92 KB
/
lists.py
File metadata and controls
102 lines (84 loc) · 1.92 KB
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
#Lists
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
#['trek', 'cannondale', 'redline', 'specialized']
#List Access
#First
print(bicycles[0])
#trek
#Last
print(bicycles[-1])
#specialized
#Modification
bicycles[0] = 'training'
print(bicycles[0])
#training
#Adding
bicycles.append('mountain')
print(bicycles[-1])
#mountain
#Inserts value at 0 and shifts all elements to the right
bicycles.insert(0, 'toddler')
print(bicycles[0])
#toddler
#Removal of list items
del bicycles[0]
print(bicycles)
# ['training', 'cannondale', 'redline', 'specialized', 'mountain']
#Popping from tail
bicycles.pop()
print(bicycles)
# ['training', 'cannondale', 'redline', 'specialized']
#Popping from inside list; all elements to right shift to left
bicycles.pop(1)
print(bicycles)
# ['training', 'redline', 'specialized']
#Remove by value
bicycles.remove('training')
print(bicycles)
#['redline', 'specialized']
#Sort the list
bicycles.sort(reverse = True)
print(bicycles)
#['specialized', 'redline']
#Returns a sorted list without sorting the list itself
print(sorted(bicycles))
print(bicycles)
#['redline', 'specialized']
#['specialized', 'redline']
#Reverse index order
bicycles.reverse()
print(bicycles)
#['redline', 'specialized']
#List length
print(len(bicycles))
#2
#Multiple assignment of list values to variables
cat = ['fat', 'gray', 'loud']
size, color, disposition = cat
print(size)
print(color)
print(disposition)
#Enumerating a list
for index, item in enumerate(cat):
print(index)
print(item)
#Get a random value
import random
print(random.choice(cat))
#Randomly sort the list
random.shuffle(cat)
print(cat)
#Convert back and forth to immutable tuple
t = tuple(cat)
print(t)
l = list(t)
print(l)
#Copy vs Deep Copy - Deep copy does inner lists. These methods create a new reference, so they can be changed
#independently of each other
c = [['fat', 'loud'], 1, 2]
import copy
c2 = copy.copy(c)
c2.append(3)
print(c)
print(c2)