Unpacking a dictionary's tuple keys into individual keys using dictionary comprehension in Python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Assuming a dictionary where the keys are tuples:
D = {
('a','b') : 1,
('x','y','z') : 2
}
How can I split each tuple-key into separate keys with the same value:
N = {
'a' : 1, 'b' : 1,
'x' : 2, 'y' : 2, 'z' : 2
}
In a single dictionary comprehension. I've drafted up the following line, but I'm wondering if it's possible to shorten it and not create a proxy value list of the same length as the key tuple.
N = { k:v for s in ( zip(keys,[value]*len(keys)) for keys,value in D.items() ) for k,v in s }
python dictionary key
add a comment |
Assuming a dictionary where the keys are tuples:
D = {
('a','b') : 1,
('x','y','z') : 2
}
How can I split each tuple-key into separate keys with the same value:
N = {
'a' : 1, 'b' : 1,
'x' : 2, 'y' : 2, 'z' : 2
}
In a single dictionary comprehension. I've drafted up the following line, but I'm wondering if it's possible to shorten it and not create a proxy value list of the same length as the key tuple.
N = { k:v for s in ( zip(keys,[value]*len(keys)) for keys,value in D.items() ) for k,v in s }
python dictionary key
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55
add a comment |
Assuming a dictionary where the keys are tuples:
D = {
('a','b') : 1,
('x','y','z') : 2
}
How can I split each tuple-key into separate keys with the same value:
N = {
'a' : 1, 'b' : 1,
'x' : 2, 'y' : 2, 'z' : 2
}
In a single dictionary comprehension. I've drafted up the following line, but I'm wondering if it's possible to shorten it and not create a proxy value list of the same length as the key tuple.
N = { k:v for s in ( zip(keys,[value]*len(keys)) for keys,value in D.items() ) for k,v in s }
python dictionary key
Assuming a dictionary where the keys are tuples:
D = {
('a','b') : 1,
('x','y','z') : 2
}
How can I split each tuple-key into separate keys with the same value:
N = {
'a' : 1, 'b' : 1,
'x' : 2, 'y' : 2, 'z' : 2
}
In a single dictionary comprehension. I've drafted up the following line, but I'm wondering if it's possible to shorten it and not create a proxy value list of the same length as the key tuple.
N = { k:v for s in ( zip(keys,[value]*len(keys)) for keys,value in D.items() ) for k,v in s }
python dictionary key
python dictionary key
edited Nov 16 '18 at 19:52
dexgecko
asked Nov 16 '18 at 19:45
dexgeckodexgecko
1,4291817
1,4291817
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55
add a comment |
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55
add a comment |
2 Answers
2
active
oldest
votes
The nested comprehension you are looking for looks like this:
>>> D = {
...: ('a','b') : 1,
...: ('x','y','z') : 2
...:}
>>>
>>> {k_i:v for k, v in D.items() for k_i in k}
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
which could be written with traditional for
loops like this:
>>> result = {}
>>> for k, v in D.items():
...: for k_i in k:
...: result[k_i] = v
...:
>>> result
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
Bonus: itertools
abuse!
>>> from itertools import repeat, chain
>>> dict(chain.from_iterable(zip(k, repeat(v)) for k, v in D.items()))
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
add a comment |
You can use a dict comprehension like this:
{k: v for t, v in D.items() for k in t}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344395%2funpacking-a-dictionarys-tuple-keys-into-individual-keys-using-dictionary-compre%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The nested comprehension you are looking for looks like this:
>>> D = {
...: ('a','b') : 1,
...: ('x','y','z') : 2
...:}
>>>
>>> {k_i:v for k, v in D.items() for k_i in k}
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
which could be written with traditional for
loops like this:
>>> result = {}
>>> for k, v in D.items():
...: for k_i in k:
...: result[k_i] = v
...:
>>> result
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
Bonus: itertools
abuse!
>>> from itertools import repeat, chain
>>> dict(chain.from_iterable(zip(k, repeat(v)) for k, v in D.items()))
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
add a comment |
The nested comprehension you are looking for looks like this:
>>> D = {
...: ('a','b') : 1,
...: ('x','y','z') : 2
...:}
>>>
>>> {k_i:v for k, v in D.items() for k_i in k}
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
which could be written with traditional for
loops like this:
>>> result = {}
>>> for k, v in D.items():
...: for k_i in k:
...: result[k_i] = v
...:
>>> result
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
Bonus: itertools
abuse!
>>> from itertools import repeat, chain
>>> dict(chain.from_iterable(zip(k, repeat(v)) for k, v in D.items()))
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
add a comment |
The nested comprehension you are looking for looks like this:
>>> D = {
...: ('a','b') : 1,
...: ('x','y','z') : 2
...:}
>>>
>>> {k_i:v for k, v in D.items() for k_i in k}
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
which could be written with traditional for
loops like this:
>>> result = {}
>>> for k, v in D.items():
...: for k_i in k:
...: result[k_i] = v
...:
>>> result
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
Bonus: itertools
abuse!
>>> from itertools import repeat, chain
>>> dict(chain.from_iterable(zip(k, repeat(v)) for k, v in D.items()))
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
The nested comprehension you are looking for looks like this:
>>> D = {
...: ('a','b') : 1,
...: ('x','y','z') : 2
...:}
>>>
>>> {k_i:v for k, v in D.items() for k_i in k}
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
which could be written with traditional for
loops like this:
>>> result = {}
>>> for k, v in D.items():
...: for k_i in k:
...: result[k_i] = v
...:
>>> result
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
Bonus: itertools
abuse!
>>> from itertools import repeat, chain
>>> dict(chain.from_iterable(zip(k, repeat(v)) for k, v in D.items()))
>>> {'a': 1, 'b': 1, 'x': 2, 'y': 2, 'z': 2}
edited Nov 16 '18 at 20:02
answered Nov 16 '18 at 19:46
timgebtimgeb
51.4k126795
51.4k126795
add a comment |
add a comment |
You can use a dict comprehension like this:
{k: v for t, v in D.items() for k in t}
add a comment |
You can use a dict comprehension like this:
{k: v for t, v in D.items() for k in t}
add a comment |
You can use a dict comprehension like this:
{k: v for t, v in D.items() for k in t}
You can use a dict comprehension like this:
{k: v for t, v in D.items() for k in t}
answered Nov 16 '18 at 19:47
blhsingblhsing
44.2k51745
44.2k51745
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344395%2funpacking-a-dictionarys-tuple-keys-into-individual-keys-using-dictionary-compre%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
(Note that for this question to be well defined the tuple-keys must be pairwise disjoint.)
– timgeb
Nov 16 '18 at 19:55