Rik
Rik

Reputation: 1987

Correctly index list of strings

I'd like to be able to index a multi-line string such as the hangman below to chose the correct string based on the number of words incorrect. When I use the code below it does not index the correct figure and I'm not sure why. Thanks.

a=list({

'''
     |
     O
    \|


''',

'''
     |
     O
    \|/
     |

''',

'''
     |
     O
    \|/
     |
    /
''',

'''
     |
     O
    \|/
     |
    / \
'''
});

for i in range(4):
    print(a[i])

Output:

     |
     O
    \|/
     |



     |
     O
    \|/




     |
     O
    \|/
     |
    /

     |
     O
    \|/
     |
    /

Upvotes: 1

Views: 49

Answers (2)

Marvo
Marvo

Reputation: 520

a=[

'''
     |
     O
    \|


''',

'''
     |
     O
    \|/
     |

''',

'''
     |
     O
    \|/
     |
    /
''',

'''
     |
     O
    \|/
     |
    / \\
'''
]

for i in range(4):
    print(a[i])

Upvotes: 1

jez
jez

Reputation: 15349

If you want order to be preserved, don't use {} notation because this creates a fundamentally unordered container (either a set or a dict—in your case, a set). You then convert it to a list, but too late—order is already lost.

Instead, just say:

a = [
  """"
  first string
  """,
  # ...
]

Square brackets denote a list, which is an ordered container.

Upvotes: 4

Related Questions