I have a multi-dim dict with primary key being a hash and sub values keyed on single chars. I'm trying to assign a subset of values to multiple vars by iterating over a string containing the chars of the sub-keys I want. Something like:
A,B,C = tree[hash][i] for i in "xyz"
This would equate to:
A = tree[hash]["x"]
B = tree[hash]["y"]
C = tree[hash]["z"]
But trying to do it all in a single line, possibly to imbed in a function where I would pass the list of vars and the corresponding string of sub-keys.
You have nearly done that. Just add square brackets around right side expression. It's called list comprehension:
A, B, C = [tree[hash][i] for i in "xyz"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With