Skip to content

Commit 144ece9

Browse files
committed
learned more about list and deleting element in the list
1 parent 3a1b05e commit 144ece9

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

0033_deleting_list_element.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
data= [4,5,104,105,110,120, 130, 130, 150, 160, 170, 183,185,187, 188, 191, 350, 360]
2-
1+
# data= [4,5,104,105,110,120, 130, 130, 150, 160, 170, 183,185,187, 188, 191, 350, 360]
2+
# data= [4,5,104,105,110,120, 130, 130, 150, 160, 170, 183,185,187, 188, 191]
3+
# data= [104,105,110,120, 130, 130, 150, 160, 170, 183,185,187, 188, 191, 350, 360]
4+
# data= [104,105,110,120, 130, 130, 150, 160, 170, 183,185,187, 188, 191]
5+
data= []
36
# del data[0:2]
47
# print(data)
58

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
data = [104, 101, 4, 105, 308, 103, 5,
2+
107, 100, 306, 106, 102, 108]
3+
4+
min_valid = 100
5+
max_valid = 200
6+
7+
# for index in range(len(data) - 1, -1, -1):
8+
# if data[index] < min_valid or data[index] > max_valid:
9+
# print(index)
10+
# del data[index]
11+
12+
top_index = len(data) - 1
13+
for index, value in enumerate(reversed(data)):
14+
if value < min_valid or value > max_valid:
15+
print(top_index - index,value)
16+
del data[top_index - index]
17+
print(data)
18+

0035_nested_lists.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
empty_list = []
2+
even = [2, 4, 6, 8]
3+
odd = [1, 3, 5, 7, 9]
4+
5+
numbers = [even, odd]
6+
print(numbers)
7+
8+
for number_list in numbers:
9+
print(number_list)
10+
for value in number_list:
11+
print(value)
12+
13+
print()
14+
menu = [
15+
["egg", "bacon"],
16+
["egg", "sausage", "bacon"],
17+
["egg", "spam"],
18+
["egg", "bacon", "spam"],
19+
["egg", "bacon", "sausage", "spam"],
20+
["spam", "bacon", "sausage", "spam"],
21+
["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
22+
# python ignores comma at the end of the last element of the list
23+
["spam", "egg", "spam", "spam", "bacon", "spam"],
24+
]
25+
26+
for meal in menu:
27+
if "spam" not in meal:
28+
print(meal)
29+
for item in meal:
30+
print(item)
31+
32+
else:
33+
print("{0} has a spam score of {1}".format(meal, meal.count("spam")))

0036_no_spam_challenge.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
menu = [
2+
["egg", "bacon"],
3+
["egg", "sausage", "bacon"],
4+
["egg", "spam"],
5+
["egg", "bacon", "spam"],
6+
["egg", "bacon", "sausage", "spam"],
7+
["spam", "bacon", "sausage", "spam"],
8+
["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
9+
["spam", "egg", "spam", "spam", "bacon", "spam"],
10+
]
11+
12+
# removes all the occurence of spam
13+
for meal in menu:
14+
if meal.count("spam") > 1:
15+
for index in range(len(meal) - 1, -1, -1):
16+
if meal[index] == "spam":
17+
del meal[index]
18+
elif meal.count("spam") == 1:
19+
meal.remove("spam")
20+
print(meal)

0 commit comments

Comments
 (0)