Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

enumerate in python

languages = ['Python', 'C', 'C++', 'C#', 'Java']

#Bad way
i = 0 #counter variable
for language in languages:
    print(i, language)
    i+=1

#Good Way
for i, language in enumerate(languages):
    print(i, language)
Comment

enumerate in python

# Python program to illustrate
# enumerate function in loops
l1 = ["eat", "sleep", "repeat"]
  
# printing the tuples in object directly
for ele in enumerate(l1):
    print (ele)
>>>(0, 'eat')
>>>(1, 'sleep')
>>>(2, 'repeat')    
  
# changing index and printing separately
for count, ele in enumerate(l1, 100):
    print (count, ele)
>>>100 eat
>>>101 sleep
>>>102 repeat  

# getting desired output from tuple
for count, ele in enumerate(l1):
    print(count)
    print(ele)
>>>0
>>>eat
>>>1
>>>sleep
>>>2
>>>repeat    
Comment

enumerate in python

list1 = ['1', '2', '3', '4']

for index, listElement in enumerate(list1): 
    #What enumerate does is, it gives you the index as well as the element in an iterable
    print(f'{listElement} is at index {index}') # This print statement is just for example output

# This code will give output : 
"""
1 is at index 0
2 is at index 1
3 is at index 2
4 is at index 3
"""
Comment

enumerate items python

mydict = {1: 'a', 2: 'b'}
for i, (k, v) in enumerate(mydict.items()):
    print("index: {}, key: {}, value: {}".format(i, k, v))

# which will print:
# -----------------
# index: 0, key: 1, value: a
# index: 1, key: 2, value: b
Comment

enumerate in python

mydict = {1: 'a', 2: 'b'}
for i, (k, v) in enumerate(mydict.items()):
    print(i,k,v)
    
#will print   
# 0 1 a
# 1 2 b
Comment

PREVIOUS NEXT
Code Example
Python :: clear list 
Python :: how to run loops 3 times in python 
Python :: get vowels from string python 
Python :: how to draw a single pixel in pyglet 
Python :: finding random numbers python 
Python :: if else python 
Python :: root value of a column pandas 
Python :: if a list has a string remove 
Python :: random chars generator python 
Python :: how to take array as input in python 
Python :: print example 
Python :: python assert is datetime 
Python :: create app in a django project 
Python :: send api request python 
Python :: import matlab python 
Python :: Python Datetime Get year, month, hour, minute, and timestamp 
Python :: add values to tuple python 
Python :: python datetime minus datetime 
Python :: stack error: command failed: import sys; print "%s.%s.%s" % sys.version_info[:3]; 
Python :: how to append number in tuple 
Python :: switch between frames in tkinter 
Python :: balancing paranthesis python 
Python :: python excel file 
Python :: type python 
Python :: how to append to a dictionary in python 
Python :: python delete dictionary key 
Python :: python linear regression 
Python :: python check if object is empty 
Python :: get coordinates in xarray 
Python :: add horizontal line to plotly scatter 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =