Home > Software design >  Given two lists, words & definitions . We Need to create a dictionary called cooldictionary where we
Given two lists, words & definitions . We Need to create a dictionary called cooldictionary where we

Time:09-17

Below is the given sample lists

words = ["PoGo","Spange","Lie-Fi"]

definitions = ["Slang for Pokemon Go","To collect spare change, either from couches, passerbys on the street or any numerous other ways and means","When your phone or tablet indicates that you are connected to a wireless network, however you are still unable to load webpages or use any internet services with your device"] 

CodePudding user response:

Try this:

cooldictionary = {a : b for (a,b) in zip(words, definitions)}

By thanks @Danimesejo you can try this:

cooldictionary = dict(zip(words, definitions))

CodePudding user response:

Below is the code which is implemented.

words = ["PoGo","Spange","Lie-Fi"]

definitions = ["Slang for Pokemon Go","To collect spare change, either from couches, passerbys on the street or any numerous other ways and means","When your phone or tablet indicates that you are connected to a wireless network, however you are still unable to load webpages or use any internet services with your device"] 

cooldictionary = {}
for word in words:
    for definition in definitions:
        cooldictionary[word] = definition
        definitions.remove(definition)
        break
    
print(cooldictionary)

Explanation:

In the above code we have created a dictionary named cooldictionary which searches for each elements in a words dictionary and definitions dictionary and matches them in the form of key and value pair one by one for each index and removing them with every iteration untill the elements end and then it breaks the loop and print it finally.

  • Related