Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python assignment destructioning

Tags:

python

How can I do the ES6 type object destructuring in Python?

dictionary = {}

dictionary['a'] = 'hello'
dictionary['b'] = 'goodbye'

print dictionary

a, b = [dictionary]

print a, b

How can I have it print hello goodbye ?

like image 383
Anthony Avatar asked Jun 09 '26 09:06

Anthony


1 Answers

You can actually do this:

a, b = dictionary.values()

Or if you are worrying dictionary is not ordered, you can do:

from collections import OrderedDict

print "Hello World!\n"
dictionary = OrderedDict()  # <-------------

dictionary['a'] = 'hello'
dictionary['b'] = 'goodbye'

print dictionary

a, b = dictionary.values()

print a, b
like image 152
Bill Chen Avatar answered Jun 11 '26 21:06

Bill Chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!