Python Tutorials
Enumerate in Python with EXAMPLES
Sometimes you want to reference your items in the list or other data structure for later use. It makes easier by providing an enumerate function in Python.
Let’s take a look at the parameter of enumerate.
enumerate(iterable, startIndex)
- Iterable: list or other iterable.
- StartIndex: It is the starting number. StartIndex is optional.
Let’s take a look at the code.
name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]e_name = enumerate(name) print(e_name) |
The enumerate function returns an enumerate object that we need to iterate to get the output values. Let us iterate through the list.
name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]e_name = enumerate(name) for i in e_name: print(i) |
Now let’s start counting from 5 now.
name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]e_name = enumerate(name,5) for i in e_name: print(i) |
The following is the output
Enumerating a Tuple
The enumerate on tuple works the same as on lists.
name = (‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’) e_name = enumerate(name) for i in e_name: print(i) |
Enumerating a String
Let’s take a look at the code to enumerate in python string.
name = (‘Hello’) e_name = enumerate(name) for i in e_name: print(i) |
Facebook Comments