-2

Lets say I have a dictionary:

dict = {"A":"a", "B":"b"}

I would like to make a new list which is named after the value of key "B", and than pot original dictionary inside the new list.

b =(dict)

thanks!

  • You don't. You also do not name variables after built ins (`dict()` is being shadowed). Whereever you would use your new `b` use `dict_renamed` instead. If you need the value, use `dict_renamed["B"]`. – Patrick Artner Sep 23 '20 at 11:15

1 Answers1

0

You can use globals(). For your dictionary i used the name your_dict as dict could cause issues due to the built in method dict:

your_dict = {"A":"a", "B":"b"} 

globals()[your_dict['B']]=your_dict

print(b)

Output:

{"A":"a", "B":"b"}
IoaTzimas
  • 10,263
  • 2
  • 10
  • 29
  • Bad advise. It will work and do what he wants, but this kind of questions are often XY-Problems. Solving it like this breeds bad programming habits. He can use `your_dict` instead of creating an artificial `b` – Patrick Artner Sep 23 '20 at 11:18
  • and just in case it gets dv'td - wasn't me. – Patrick Artner Sep 23 '20 at 11:20