Enumerators in python & How they work? ..

Home Forums Python Programming Enumerators in python & How they work? ..

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1939 Reply
    Humble
    Keymaster

    Enumerators in python is very helpful and give “index” access to your iteratable object..

    The input for your enumerator should be an iterator .. Please watch below example.

    >>>> l=[10,3,4,5,6]
    >>> for i,j in enumerate(l):
    ...      print "%s is in index %s" %(j,i)
    ... 
    10 is in index 0
    3 is in index 1
    4 is in index 2
    5 is in index 3
    6 is in index 4
    >>> 
    
    >>>
    
    If you would like to fetch specific index , here is an example:
    
    
    >>>> for i,j in enumerate(l):
    ...     if j==1:
    ...             print "%s is at index:%s" %(j,i)
    ... 
    1 is at index:0
    1 is at index:5
    >>> 
    
Viewing 1 post (of 1 total)
Reply To: Enumerators in python & How they work? ..
Your information: