Is there any way to map a list of elements to a dictionary without a loop?
I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.
Input dictionary:
edict_all = 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]
And I have 2 lists:
list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]
Currently, I am using 2 for loops:
edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]
for i in list_b:
edict_b[i] = edict_all[i]
And the output is:
edict_a = 1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]
edict_b = 1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]
python python-3.x list dictionary mapping
add a comment |
I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.
Input dictionary:
edict_all = 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]
And I have 2 lists:
list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]
Currently, I am using 2 for loops:
edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]
for i in list_b:
edict_b[i] = edict_all[i]
And the output is:
edict_a = 1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]
edict_b = 1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]
python python-3.x list dictionary mapping
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24
add a comment |
I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.
Input dictionary:
edict_all = 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]
And I have 2 lists:
list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]
Currently, I am using 2 for loops:
edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]
for i in list_b:
edict_b[i] = edict_all[i]
And the output is:
edict_a = 1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]
edict_b = 1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]
python python-3.x list dictionary mapping
I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.
Input dictionary:
edict_all = 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]
And I have 2 lists:
list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]
Currently, I am using 2 for loops:
edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]
for i in list_b:
edict_b[i] = edict_all[i]
And the output is:
edict_a = 1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]
edict_b = 1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]
python python-3.x list dictionary mapping
python python-3.x list dictionary mapping
edited Nov 14 '18 at 17:14
jpp
102k2164115
102k2164115
asked Nov 14 '18 at 15:15
Gurpreet.SGurpreet.S
626
626
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24
add a comment |
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24
add a comment |
2 Answers
2
active
oldest
votes
Loops at some level are unavoidable here, but you can hide them with zip
and operator.itemgetter
:
from operator import itemgetter
edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))
add a comment |
You can use Python generator expression to filter data and then pass it back to dict()
constructor:
edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)
And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.
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%2f53303371%2fis-there-any-way-to-map-a-list-of-elements-to-a-dictionary-without-a-loop%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
Loops at some level are unavoidable here, but you can hide them with zip
and operator.itemgetter
:
from operator import itemgetter
edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))
add a comment |
Loops at some level are unavoidable here, but you can hide them with zip
and operator.itemgetter
:
from operator import itemgetter
edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))
add a comment |
Loops at some level are unavoidable here, but you can hide them with zip
and operator.itemgetter
:
from operator import itemgetter
edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))
Loops at some level are unavoidable here, but you can hide them with zip
and operator.itemgetter
:
from operator import itemgetter
edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))
answered Nov 14 '18 at 17:13
jppjpp
102k2164115
102k2164115
add a comment |
add a comment |
You can use Python generator expression to filter data and then pass it back to dict()
constructor:
edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)
And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.
add a comment |
You can use Python generator expression to filter data and then pass it back to dict()
constructor:
edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)
And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.
add a comment |
You can use Python generator expression to filter data and then pass it back to dict()
constructor:
edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)
And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.
You can use Python generator expression to filter data and then pass it back to dict()
constructor:
edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)
And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.
answered Nov 14 '18 at 18:10
Andrey SemakinAndrey Semakin
153211
153211
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%2f53303371%2fis-there-any-way-to-map-a-list-of-elements-to-a-dictionary-without-a-loop%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
Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.
– benvc
Nov 14 '18 at 16:24