Enumerate function in Python

 


mlist = ["abdur", "banana", "dog", "gem", "carbon", "eyes", "moon"]
index = 0
for item in mlist:
    print(index, item)
    index += 1

# printing of item names along with indices can
# be done with the enumerate function as below;

for index, item in enumerate(mlist):
    print(index, item)


Also; another example

l1 = ["tomato", "potato", "laddoo", "wresha", "chawal", "siwian"]
# return indexed list through ordinary method
i = 1
for item in l1:
    print(f"{i}. {item}")
    i += 1
# return indexed list through enumerate method
for index, item in enumerate(l1, start=1):
    print(f"{index}. {item}")









Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes