If you are iterating over the items in some container, just iterate over that container; don’t use the index unless you need to.
Right:
slots = [1, 2, 3, 7]
for slot in slots:
print cards[slot]
Wrong (or at least “writing C/C++/Java/C#/whatever in Python”):
slots = [1, 2, 3, 7]
for i in range(len(slots)):
print cards[slots[i]]
If you need an index for some other purpose (e.g. you’re going to modify the container), the Python way is enumerate()
:
for i, slot in enumerate(slots):
print cards[slot]
slots[i] += 1
solved Java to python conversion [closed]