Skip to content

Commit 0363001

Browse files
Caleb CurryCaleb Curry
authored andcommitted
yeet
1 parent 46f7662 commit 0363001

4 files changed

Lines changed: 94 additions & 96 deletions

File tree

.DS_Store

6 KB
Binary file not shown.
Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -95,88 +95,4 @@
9595
backpack.append("pizza slice")
9696
print("You put a piece of pizza in your backpack")
9797
else:
98-
print("How about you go to the gym?")
99-
100-
101-
########## INSERT INTO MIDDLE OF LIST ##########
102-
103-
104-
#Lists are ordered, and this order may matter to you.
105-
106-
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
107-
108-
#OVERTIME!
109-
work_days.insert(3, "Wednesday") #index, "data"
110-
111-
print(work_days)
112-
113-
#What if we want to do the opposite and take a day out?
114-
115-
116-
########## REMOVE ELEMENT FROM LIST BY INDEX ##########
117-
118-
119-
#We learned how to remove by value:
120-
121-
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
122-
123-
#VACATION DAY!
124-
work_days.remove("Saturday")
125-
print(work_days)
126-
127-
#However, removing by index is also useful:
128-
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
129-
130-
del work_days[0]
131-
print(work_days)
132-
133-
134-
########## REMOVE ELEMENT WITH POP ##########
135-
136-
137-
#The benefit here is the method returns the element
138-
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
139-
popped = work_days.pop(1)
140-
141-
print("You removed " + popped)
142-
print(work_days)
143-
144-
145-
########## REMOVE ELEMENT FROM LIST USING DEL AND SLICE ##########
146-
147-
148-
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
149-
150-
del work_days[0:2] #remove first 2
151-
print(work_days)
152-
153-
del work_days[-2:] #remove last 2 (start 2 from right and go to end)
154-
print(work_days)
155-
156-
157-
########## REMOVING ALL OCCURANCES IN LIST ##########
158-
159-
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
160-
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
161-
162-
backpack.remove("pizza slice")
163-
print(backpack) #SO MUCH PIZZA!
164-
165-
while(backpack.count("pizza slice") > 0):
166-
backpack.remove("pizza slice")
167-
168-
print(backpack)
169-
170-
#This may not be the most optimized solution as each removal requires
171-
#an iteration from backpack.count. But it's cleanest
172-
#You should also avoid modifying a list while iterating, so a for-in loop is bad
173-
174-
#for item in backpack:
175-
# if(item == "pizza slice"):
176-
# backpack.remove(item)
177-
178-
#The original solution is fine for removing data from reasonably sized lists
179-
#for very very large lists, look into list comprehension
180-
#Which we are not getting into quite yet.
181-
182-
98+
print("How about you go to the gym?")

python/02-lists.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
########## INSERT INTO MIDDLE OF LIST ##########
2+
3+
4+
#Lists are ordered, and this order may matter to you.
5+
6+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
7+
8+
#OVERTIME!
9+
work_days.insert(3, "Wednesday") #index, "data"
10+
11+
print(work_days)
12+
13+
#What if we want to do the opposite and take a day out?
14+
15+
16+
########## REMOVE ELEMENT FROM LIST BY INDEX ##########
17+
18+
19+
#We learned how to remove by value:
20+
21+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
22+
23+
#VACATION DAY!
24+
work_days.remove("Saturday")
25+
print(work_days)
26+
27+
#However, removing by index is also useful:
28+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
29+
30+
del work_days[0]
31+
print(work_days)
32+
33+
34+
########## REMOVE ELEMENT WITH POP ##########
35+
36+
37+
#The benefit here is the method returns the element
38+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
39+
popped = work_days.pop(1)
40+
41+
print("You removed " + popped)
42+
print(work_days)
43+
44+
45+
########## REMOVE ELEMENT FROM LIST USING DEL AND SLICE ##########
46+
47+
48+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
49+
50+
del work_days[0:2] #remove first 2
51+
print(work_days)
52+
53+
del work_days[-2:] #remove last 2 (start 2 from right and go to end)
54+
print(work_days)
55+
56+
57+
########## REMOVING ALL OCCURANCES IN LIST ##########
58+
59+
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
60+
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
61+
62+
backpack.remove("pizza slice")
63+
print(backpack) #SO MUCH PIZZA!
64+
65+
while(backpack.count("pizza slice") > 0):
66+
backpack.remove("pizza slice")
67+
68+
print(backpack)
69+
70+
#This may not be the most optimized solution as each removal requires
71+
#an iteration from backpack.count. But it's cleanest
72+
#You should also avoid modifying a list while iterating, so a for-in loop is bad
73+
74+
#for item in backpack:
75+
# if(item == "pizza slice"):
76+
# backpack.remove(item)
77+
78+
#The original solution is fine for removing data from reasonably sized lists
79+
#for very very large lists, look into list comprehension
80+
#Which we are not getting into quite yet.
81+
82+
83+
########## REVERSE LIST ##########
84+
85+
86+
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
87+
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
88+
89+
print(backpack)
90+
backpack.reverse()
91+
print(backpack)
92+
93+
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
########## REVERSE LIST ##########
2-
3-
4-
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
5-
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
6-
7-
print(backpack)
8-
backpack.reverse()
9-
print(backpack)
10-
11-
121
########## SORT METHOD ##########
132

143

0 commit comments

Comments
 (0)