Enumerate is a Python built-in method. Enumerate() takes a set (e.g. a tuple) and returns it like an entity of enumeration. In a small statement, its significance can not be described. Although it is unfamiliar to most beginners, as well as some proficient programmers. It enables one to loop and provide an auto-counter about something. A counter is inserted by the enumerate() method as the enumerate object key.
Enumerate() Function Syntax
You would need to use the enumerate() method with two parameters to invoke enumerate() in Python.
Iterable: An entity that is loopable
StartIndex(optional): The countdown begins with the value assigned in the startIndex for, perhaps, the first element in the loop and increments it for the next item until it hits the loop edge.
Note: If startIndex is not defined, the count begins at 0.
Enumerate() working
Enumerate() function can work to enumerate over a tuple, list, string, for loops, and dictionaries since all of these are iterable things.
Enumerating a list
Example 01:
Here, we have a simple example of enumeration on a list without a startIndex, which is why indexes will be assigned from 0. We have defined a simple list ‘x’ and enumerate it using the function, and simply print it down.
It is clear that the indexes are assigned and the starting point is 0.
Example 02:
Using the same example in example 01, the illustration below performs enumeration while initializing the start index as 10.
Indexes are assigned, and starting from 10.
Example 03:
You can also define the start index as startIndex = any number.
It will also give a similar output as shown.
Example 04:
You can also check the type of a given object using the ‘type’ keyword in the method. Here is a simple model of a list having 4 seasons mentioned. The list ‘season’ has been converted into an enumerating class and saved into variable ‘obj1’. In the third statement, the object type of this list has been printed. At the next line, this enumerated ‘obj1’ will be printed out. After that, list season has been enumerated and converted again into a list while printing.
You can see that the enumerated ‘obj1’ has an address assigned while the generator object has been created.
Enumerating a Tuple
Enumerating a tuple is most likely to be similar to enumerating a list. Here is an example:
Example 01:
Taking the same example for tuple, we have printed out all the enumerated tuples while converting them to a list again. We have both with and without startIndex statements here.
Enumerated tuples have been printed, with and without the starting index number, as below.
Enumerating a String
We can use the enumerate function on string type variables because strings are also iterable. Let’s have a glimpse of some examples of strings.
Example 01:
Initialize a string with some name and assign some value to it, as we have assigned the ‘AqsaYasin’ value to the variable ‘string’. In the second line, we have enumerated this string and converted it into a list, then printed it out. In the third line, we have printed the string with enumeration again, without converting it into a list.
The below output will be generated. The first line of output shows the enumerated and converted it into the list format of the string, while the other line only shows the enumerated generator object address of the string.
Enumerating through a loop
You can enumerate several variable and data types using the loop in the code. Here are some examples to elaborate it clearly:
Example 01:
Let us use the same example again, with just a little amendment in it. We have defined a tuple ‘x’, enumerated it in the ‘for’ loop, and then printed it out. You can also use ‘i’ instead of the tuple name right after the for loop started.
You can see how the for loop works in the output line by line while enumerating some variable types.
Example 02:
Taking the same old example with and without taking the start index number in a ‘for’ loop, we have to print the list while enumerating it, as in the picture below. Print statement having backslash ‘\n’ is used to make one line jump in the output.
The list has been enumerated first, then printed. There is a line jump in the output for the first print statement that started from 0 indexes. The last print statement has index numbers starting 10 onwards.
Example 03:
Now we have to enumerate a string using the for loop in our example. So let’s have a look at the code shown in the picture. We have a string with the value ‘Aqsa Yasin’. We have enumerated this string using the enumerate function in for loop, then print it.
You will get a similar output, as shown in the attached photo. Every character and space has been assigned with some index number.
Example 04:
We are going to enumerate a dictionary using the for loop in this example. So let’s have a look at the example shown in the attached picture. We have a dictionary named ‘Place’, with some values in it as the country’s names. We have enumerated this dictionary using the enumerate method in for loop, then print it.
The output will be a little similar to the image below. You can see the keys defined in the dictionary have been assigned indexes separately to each one.
Example 05:
Perform a simple check on index number 0 of a list having users in it as shown. If the index number is 0, then it will print the user located at this index.
The located user at index 0 is ‘Aqsa’, so it will be printed.
Conclusion
This implies that you do not need a count variable to reach the iterable objects. Often, however, you do need to provide a variable to every loop iteration that varies. You should use Python’s enumerate() instead of generating and incrementing a variable manually to simultaneously have a counter as well as the output from the iterable!
from Linux Hint https://ift.tt/2Y1SCbM
0 Comments