-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17_list_manipulation.py
More file actions
65 lines (49 loc) · 1.67 KB
/
17_list_manipulation.py
File metadata and controls
65 lines (49 loc) · 1.67 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
# 17 - List Manipulations (Slicing, Subsetting, Modifying)
# --- Basic Slicing ---
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[3:]) # [40, 50, 60]
print(numbers[-3:]) # [40, 50, 60]
print(numbers[:-2]) # [10, 20, 30, 40]
print(numbers[::2]) # [10, 30, 50]
print(numbers[::-1]) # [60, 50, 40, 30, 20, 10]
# Copying a list (shallow copy)
copy = numbers[:]
print(copy) # [10, 20, 30, 40, 50, 60]
# --- Subsetting Lists (Nested or Indexed Access) ---
nested = [[1, 2], [3, 4], [5, 6]]
print(nested[1]) # [3, 4]
print(nested[1][0]) # 3
# Mixed data types and indexing
data = [100, "text", True, [9, 8]]
print(data[3][1]) # 8
# --- List Manipulation (Mutating Methods) ---
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add to end
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
fruits.insert(1, "grape") # Insert at index 1
print(fruits) # ['apple', 'grape', 'banana', 'cherry', 'orange']
fruits.remove("banana") # Remove by value
print(fruits) # ['apple', 'grape', 'cherry', 'orange']
last = fruits.pop() # Remove last element
print(last) # orange
print(fruits) # ['apple', 'grape', 'cherry']
# Replace element by index
fruits[0] = "kiwi"
print(fruits) # ['kiwi', 'grape', 'cherry']
# Extend list with another list
fruits.extend(["melon", "berry"])
print(fruits) # ['kiwi', 'grape', 'cherry', 'melon', 'berry']
# Sorting and reversing
numbers = [3, 1, 4, 1, 5]
numbers.sort()
print(numbers) # [1, 1, 3, 4, 5]
numbers.reverse()
print(numbers) # [5, 4, 3, 1, 1]
# Deleting elements
del numbers[2] # Remove by index
print(numbers) # [5, 4, 1, 1]
# Clear all elements
numbers.clear()
print(numbers) # []