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.
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')]
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.
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')]