-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_practice_programs.py
More file actions
105 lines (65 loc) · 1.74 KB
/
list_practice_programs.py
File metadata and controls
105 lines (65 loc) · 1.74 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
103
104
105
# Create a List:
my_list = [1, 2, 3, 4, 5]
# Access an Element by Index:
my_list = [1, 2, 3, 4, 5]
print(my_list[2]) # Outputs: 3
# Add an Element to the List:
my_list = [1, 2, 3]
my_list.append(4)
# Remove an Element from the List:
my_list = [1, 2, 3]
my_list.remove(2)
# Iterate Over a List:
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
# Find the Length of a List:
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
# Check if an Element is in the List:
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 is in the list")
# Concatenate Two Lists:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
# Sort a List:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort()
# Reverse a List:
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
# Count Occurrences of an Element in a List:
my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
count = my_list.count(3)
# Find the Index of an Element in a List:
my_list = [10, 20, 30, 40, 50]
index = my_list.index(30)
# Slice a List:
my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[1:4] # Returns [2, 3, 4]
# Copy a List:
original_list = [1, 2, 3]
copied_list = original_list.copy()
# Remove Duplicates from a List:
my_list = [1, 2, 2, 3, 3, 4, 4]
unique_list = list(set(my_list))
# Find the Maximum and Minimum Element in a List:
my_list = [4, 2, 9, 7, 5, 1]
max_value = max(my_list)
min_value = min(my_list)
# Add Two Lists Element-wise:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = [x + y for x, y in zip(list1, list2)]
# Check if a List is Empty:
my_list = []
if not my_list:
print("The list is empty")
# Find the Sum of All Elements in a List:
my_list = [1, 2, 3, 4, 5]
total = sum(my_list)
# Remove the Last Element from a List:
my_list = [1, 2, 3, 4, 5]
my_list.pop()