Enumerate () function in Python
In Python, the enumerate () function adds a counter before each iterable and returns the result as an enumerate object. The article will elaborate on the syntax, parameters and usage of the function. Invites you to read the track.
Enumerate () function syntax in Python
enumerate(iterable, start=0)
Parameters of the enumerate () function
- iterable: string, list, tuple, iterator or any object that supports iteration.
- start: enumerate () starts the counter from this number. If the start parameter is omitted, 0 will be the default value assigned.
Value returned from enumerate ()
The enumerate () function adds a counter before each iterable and returns the result as a listed object. These enumerate objects can then be used directly in loops or converted into a list, a tuple by list () and tuple () methods .
Example 1: The enumerate () function works in Python
cuahang = ['banhmi', 'sua', 'keo']
enumerateCuahang = enumerate(cuahang)
print(type(enumerateCuahang))
# chuyển đổi thành một danh sách
# viết bởi TipsMake.com
print(list(enumerateCuahang))
# bộ đếm chỉ định số bắt đầu
# viết bởi TipsMake.com
enumerateCuahang = enumerate(cuahang, 10)
print(list(enumerateCuahang))
Run the program, the result is:
[(0, 'banhmi'), (1, 'sua'), (2, 'keo')]
[(10, 'banhmi'), (11, 'sua'), (12, 'keo')]
Example 2: Loop on the enumerate object
cuahang = ['banhmi', 'sua', 'keo']
for item in enumerate(cuahang):
print(item)
print('n')
for count, item in enumerate(cuahang):
print(count, item)
print('n')
# bộ đếm chỉ định số bắt đầu
# viết bởi TipsMake.com
for count, item in enumerate(cuahang, 100):
print(count, item)
Run the program, the result is:
(0, 'banhmi')
(1, 'sua')
(2, 'keo')
0 banhmi
1 sua
2 keo
100 banhmi
101 sua
102 keo
Previous lesson: divmod () function in Python
Next lesson: eval () function in Python
4 ★ | 2 Vote
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.