+ 1
where you will probably use enumerate function. And why?
enumerate is great to get numerated, ordered list (not sorted). So I wonder does developers use it and how often?
1 Respuesta
+ 7
Shadoff ,
enumerate() can be used when iterating on a collection like list. we can get not only the value of the list, but also a number. the number that enumerate is giving, starts with 0 by default. but we can also define an other number to start with. for each element in the list the number will be incremented by 1.
in some cases we need to have the index of of an element. this index helps us to do operations with a specific item:
lst = ['a', 'b', 'c', 'b', 'a', 'a']
find = 'a'
for ind, char in enumerate(lst):
if char == find:
print(f"character <{char}> found at index {ind}")
result:
character <a> found at index 0
character <a> found at index 4
character <a> found at index 5
[Program finished]