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():
for index, item in enumerate(b):
print index, 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:
for index in range(len(b)):
print index, b[index]
Solution 2:
b=["hi","hello","howdy"]
for count,i in enumerate(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?"