0

I have a dict

dict = {
   'key1': 'value1',
   'key2': 'value2' 
}

and would like to unpack it to

key1 = 'value1'
key2 = 'value2'

I know that if I do

def some_func(key1, key2):
    # do stuff with key1 and key2

and then call

some_func(**dict)

then in my function I will have access to those variables.

Is there a way to get these values out without having to call the function with the dict as an argument or requiring:

key1 = dict['key1']
key2 = dict['key2']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3841621
  • 140
  • 1
  • 8
  • 1
    Why would you want to do that? You already have a namespace, the dictionary itself. – Martijn Pieters Nov 20 '14 at 17:47
  • The referenced question isn't quite what you want. You can do `globals().update(my_dict)`. – tdelaney Nov 20 '14 at 18:07
  • Also see [_Why you don't want to dynamically create variables_](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – martineau Nov 20 '14 at 18:52

0 Answers0