1

I want my list which is currently: [0] to increase by 1 each time the loop repeats.

card_number = ['A','2','3','4','5','6','7','8','9','10']

for i in range(10):
    print(card_number[0])

I want it to print each string in my list - eg: A, 2 ,3 , 4...

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Jason7261
  • 135
  • 5

3 Answers3

4

Using in operator on list itself, python automatically iterates list for you.

card_number = ['A','2','3','4','5','6','7','8','9','10']

for i in card_number:
    print(i)
Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
2

Tip: you shouldn't use 10, you should use range(len(card_number))

But I prefer this mode

card_number = ['A','2','3','4','5','6','7','8','9','10']

for c in card_number:
    print(c)
Wonka
  • 1,548
  • 1
  • 13
  • 20
1

Debugging:

for i in range(10):
    print(card_number[0])  # notice this line

It prints the 0th element, that is A in the list for 10 times, certainly not what you intended.

Hence:

Use the counter variable i instead of the index value 0:

card_number = ['A','2','3','4','5','6','7','8','9','10']

for i in range(len(card_number)):  # Use len(card_number) instead to avoid accidents
    print(card_number[i], end = " ")

Suggested (one-liner):

Using in operator, that works for each element in the list.

print(', '.join([elem for elem in card_number]))

OUTPUT:

A, 2, 3, 4, 5, 6, 7, 8, 9, 10
DirtyBit
  • 16,613
  • 4
  • 34
  • 55