Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linking multiple lists with a variable

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading