Linking multiple lists with a variable

Question:

I’m trying to link multiple lists with a variable. With the output being an item from one of the ‘multiple’ lists. The variable needs to have the name of the list. So that the index of the item in the one list is the same as the index of the item in one of the others. Sorry if it’s a duplicate, but I couln’t find anything that I can understand.

creature_type = "easy"
creature_type = "medium"
creature_type = "hard"

list1 = ['slime', 'dog', 'chicken']
list2 = ['orc', 'wolf']
list3 = ['dragon', 'golem', 'vampire']

attack4 = ['spits juice', 'bites', 'pecks']
attack5 = ['slams', 'howls']
attack6 = ['breaths fire', 'throws rocks', 'transforms']

With as output the attack for the creature in the first three lists.

Asked By: Victor van Lent

||

Answers:

Use dictionaries and zip:

creatures = {'easy': ['slime', 'dog', 'chicken'],
             'medium': ['orc', 'wolf'],
             'hard': ['dragon', 'golem', 'vampire']}

attacks = {'easy': ['spits juice', 'bites', 'pecks'],
           'medium': ['slams', 'howls'],
           'hard': ['breaths fire', 'throws rocks', 'transforms']}

choice = 'medium'

linked = list(zip(creatures[choice], attacks[choice]))

Output:

[('orc', 'slams'), ('wolf', 'howls')]
Answered By: mozway
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.