-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumerate() in python
More file actions
159 lines (118 loc) · 3.32 KB
/
enumerate() in python
File metadata and controls
159 lines (118 loc) · 3.32 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""The `enumerate()` function in Python is a built-in utility that allows you to loop through an iterable
(like a list, tuple, or string) and get both the index and the corresponding value in each iteration.
This is particularly useful when you need both the position and the element during a loop.
---
#Syntax:-"""
enumerate(iterable, start=0)
"""
- iterable: The object you want to iterate over (e.g., list, tuple, string, etc.).
- start: The starting index (default is `0`).
---
How It Works:-
enumerate() returns an enumerate object, which is an iterator yielding pairs of an index and the corresponding value from the iterable.
"""
#Basic Example:-
fruits = ['apple', 'banana', 'cherry']
for index, value in enumerate(fruits):
print(f"Index {index}: {value}")
"""Output:
Index 0: apple
Index 1: banana
Index 2: cherry
---
- Using the start Parameter
You can specify a custom starting index using the start argument.
"""
fruits = ['apple', 'banana', 'cherry']
for index, value in enumerate(fruits, start=1):
print(f"Position {index}: {value}")
"""
Output:-
Position 1: apple
Position 2: banana
Position 3: cherry
---
*Practical Use Cases:-
1. Processing Lists with Indices
Instead of manually tracking the index with a counter, you can use enumerate():
"""
names = ['Alice', 'Bob', 'Charlie']
for i, name in enumerate(names):
print(f"Processing {name} at index {i}")
---
"""
2. **Modifying Elements by Index**
If you need to update elements at specific indices:
"""
numbers = [10, 20, 30]
for i, value in enumerate(numbers):
numbers[i] = value * 2
print(numbers)
"""
Output:-
[20, 40, 60]
---
3. **Combining Enumerate with Other Iterables**
When iterating over multiple iterables:
"""
questions = ['What is your name?', 'How old are you?', 'Where do you live?']
answers = ['Alice', '25', 'Paris']
for i, (question, answer) in enumerate(zip(questions, answers), start=1):
print(f"Q{i}: {question}")
print(f"A{i}: {answer}\n")
"""
Output:-
Q1: What is your name?
A1: Alice
Q2: How old are you?
A2: 25
Q3: Where do you live?
A3: Paris
---
4. **Working with Strings**
You can iterate over characters in a string with their positions:
"""
message = "hello"
for i, char in enumerate(message):
print(f"Character at index {i}: {char}")
"""
Output:-
Character at index 0: h
Character at index 1: e
Character at index 2: l
Character at index 3: l
Character at index 4: o
---
5. **Filtering with Indices**
Use `enumerate()` to process specific items based on their indices:
"""
numbers = [10, 20, 30, 40, 50]
for i, num in enumerate(numbers):
if i % 2 == 0:
print(f"Index {i} has value {num}")
"""
Output:-
Index 0 has value 10
Index 2 has value 30
Index 4 has value 50
---
*Common Mistakes:-
1. Forgetting to Unpack Values:"""
items = ['a', 'b', 'c']
for pair in enumerate(items):
print(pair) # Returns tuples
"""
**Output**:
```
(0, 'a')
(1, 'b')
(2, 'c')
```
To unpack values:"""
for i, value in enumerate(items):
print(i, value)
"""
*Performance Note:-
- enumerate() is efficient and works well with large iterables because it returns an iterator instead of creating a separate list of indices.
With its simplicity and versatility, enumerate() is an essential function in Python, especially for working with loops where index tracking is required.
"""