Skip to content Skip to sidebar Skip to footer

How Can I Iterate Over A List Of Strings With Ints In Python?

I'm new to programming with python and programming in general and got stuck wit the following problem: b=['hi','hello','howdy'] for i in b: print i #This code outputs: hi hell

Solution 1:

The Pythonic way would be with enumerate():

forindex, item in enumerate(b):
    printindex, item

There's also range(len(b)), but you almost always will retrieve item in the loop body, so enumerate() is the better choice most of the time:

forindex in range(len(b)):
    printindex, b[index]

Solution 2:

b=["hi","hello","howdy"]
for count,i inenumerate(b):
    print count

Solution 3:

you could always do this:

b=["hi","hello","howdy"]
for i in range(len(b)):
    print i

Post a Comment for "How Can I Iterate Over A List Of Strings With Ints In Python?"