![]() Iterate through the string data using the enumerate function to determine the index of each character. ![]() # getting the enumerate object of the input tuple InputTuple = ("hello", "tutorialspoint", "python", "codes") The following program shows how to use enumerate() function on the given input tuple − # input tuple Using enumerate() function on a Tuple Example On executing, the above program will generate the following output − (0, 'hello') # printing the corresponding iterable item # traversing through each item in an enumerate object The following program shows how to loop through an enumerate object using a for loop and print each element of it − # input list Python code to loop through an enumerate object Example This second parameter will indicate the starting index in the enumerator object for the keys(indices), hence we can see the first index is 15 followed by the second index as 16 and so on in the output. In the above example, we have added 15 as the second parameter in the enumerate() function. # converting the enumerate object into the list and printing it # getting the enumerate object starting from the index 15 The following program shows how the enumerate() function is applied on a list including the second parameter − # input list Using the second parameter ‘start index’ of the enumerate() function Example On executing, the above program will generate the following output − # converting the enumerate object into a list and printing it # getting the enumerate object of the input list The following program uses enumerate() function on lists to get a list of tuples with indices − # input list It is important to note which data type will be used to store the enumerated object after it is returned. How to Use enumerate() function in Python? The syntax is as follows − enumerate(iterable, start) However, we have a pythonic approach of determining the number of iterations for necessary. When working with iterators, we always need to know how many iterations have been completed i,e, the count of iterations. The enumerate function assigns an index of each iterable value to the list. The enumerate() function is used if they want the index of iterate values. If we ignore this value it will consider the first index as zero, as zero is the default value here Start − as the name suggests, the starting index of an enumerate object is defined by start. Iterable − the data collection that is passed in so that it can be returned as enumerate object is called iterable The key is the corresponding index of each item, and the value is the items. ![]() The enumerate object is returned in key-value pair format. Python's enumerate() function accepts a data collection as a parameter and returns an enumerate object. In this article, we will learn about the enumerate() function and what are the uses of the "enumerate()" function in Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |