-1

Here is the data I have I want to loop over the two lists the symbol list as the key and symbol_name as the value how do I do it with using the loop

calculator_dict = {}   # the new dictionary i want to put both the list into

symbols = ["+", "-", "*", "/"]

symbol_name = ["add", "subtract", "multiply", "divide"]

for symbol in symbols:

  for name in symbol_name:

    calculator_dict[symbol] = name

print(calculator_dict)

What i want to be printed is:

calculator_dict = {
    "+": "add",
    "-": "subtract",
    "*": "multiply",
    "/": "divide",
}
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
Mohamed
  • 425
  • 4
  • 14
  • 1
    Does this answer your question? [How do I convert two lists into a dictionary?](https://stackoverflow.com/questions/209840/how-do-i-convert-two-lists-into-a-dictionary) – Fred Mar 05 '21 at 00:24

1 Answers1

2

Use the zip and dict functions:

>>> symbols = ["+", "-", "*", "/"]
>>> symbol_name = ["add", "subtract", "multiply", "divide"]
>>> dict(zip(symbols, symbol_name))
{'+': 'add', '-': 'subtract', '*': 'multiply', '/': 'divide'}
Samwise
  • 68,105
  • 3
  • 30
  • 44