Skip to content

Commit 3486939

Browse files
committed
Add list and del statement.
1 parent 0d9fe83 commit 3486939

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

data_structures/del/del.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
n = 100
2+
print(n)
3+
del n
4+
# print(n) # NameError
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cubes = []
2+
for i in range(20):
3+
cubes.append(i ** 3)
4+
5+
print(cubes)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
colors = ['red', 'green', 'black', 'cyan']
2+
3+
print(colors) # just print the list...
4+
5+
# add an item to the end of the list.
6+
colors.append('green')
7+
print(colors)
8+
9+
# add all of elements of iterable to the end of the list.
10+
colors.extend(['orange', 'white'])
11+
print(colors)
12+
13+
# insert an item to the specified position of the list.
14+
colors.insert(2, 'blue')
15+
print(colors)
16+
17+
# remove the first item that is equal to the given item.
18+
colors.remove('cyan')
19+
print(colors)
20+
21+
# delete the item given position from the list and return deleted item.
22+
print(colors.pop(5))
23+
print(colors)
24+
25+
# return the first position whose value equals given item.
26+
print(colors.index('black'))
27+
28+
# return the number of given item appears in the list.
29+
print(colors.count('green'))
30+
print(colors.count('lapis lazuli'))
31+
32+
# sort the list
33+
colors.sort()
34+
print(colors)
35+
36+
# reverse the items
37+
colors.reverse()
38+
print(colors)
39+
40+
# return a shallow copy of the list
41+
copy = colors.copy()
42+
print(copy)
43+
44+
# clear all of items in the list.
45+
colors.clear() # eqivalent to `del colors[:]`.
46+
print(colors)

data_structures/list_type/queue.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# collections: Container Datatypes
2+
# deque -- double-ended queue
3+
from collections import deque
4+
5+
queue = deque(['beta','gamma', 'delta'])
6+
print(queue)
7+
8+
queue.append('epsilon')
9+
print(queue)
10+
11+
queue.appendleft('alpha')
12+
print(queue)
13+
14+
queue.pop()
15+
print(queue)
16+
17+
queue.popleft()
18+
print(queue)
19+

data_structures/list_type/stack.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
stack = [10, 20, 30] # 10, 20, 30
2+
stack.append(40) # 10, 20, 30, 40
3+
stack.append(50) # 10, 20, 30, 40, 50
4+
5+
print(stack)
6+
7+
a = stack.pop()
8+
print(a)
9+
10+
a = stack.pop()
11+
print(a)
12+
13+
a = stack.pop()
14+
print(a)
15+
16+
print(stack)

0 commit comments

Comments
 (0)