nested dictionary from dict1 and dict2 using keys from dict1 and values from dict2(which are keys in dict1)











up vote
-1
down vote

favorite












users = {'196': ('110', '1'), '186': ('269', '1'), '22': ('68', '4'), '196': ('650', '3')}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)', '68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}


My code to create a nested dictionary with keys a unique list of users (keys from users) but using keys from movies to replace values of movies IDs (first item of value list from users) and keeping scores (second item of value list from users) is:




users_preference = {k: list(set().union((*map(lambda x: [x for x in movies.values()], v[0])) )) for k, v in users.items() }




But this returns all movies for each user, and I don't know how to add scores to this. Could you please help? Thank you.



The expected output is similar to:



users_preference = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}









share|improve this question
























  • It would be very helpful if you posted your expected output instead of just describing it in words.
    – timgeb
    2 days ago










  • Why do these users have a 2-tuple with '1'?
    – Willem Van Onsem
    2 days ago










  • Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
    – LMHull
    2 days ago












  • @LMHull edit the expected output into your question, please. Don't post it as a comment.
    – timgeb
    2 days ago






  • 1




    There is a duplicated key on the users dictionary '196'.
    – Daniel Mesejo
    2 days ago















up vote
-1
down vote

favorite












users = {'196': ('110', '1'), '186': ('269', '1'), '22': ('68', '4'), '196': ('650', '3')}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)', '68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}


My code to create a nested dictionary with keys a unique list of users (keys from users) but using keys from movies to replace values of movies IDs (first item of value list from users) and keeping scores (second item of value list from users) is:




users_preference = {k: list(set().union((*map(lambda x: [x for x in movies.values()], v[0])) )) for k, v in users.items() }




But this returns all movies for each user, and I don't know how to add scores to this. Could you please help? Thank you.



The expected output is similar to:



users_preference = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}









share|improve this question
























  • It would be very helpful if you posted your expected output instead of just describing it in words.
    – timgeb
    2 days ago










  • Why do these users have a 2-tuple with '1'?
    – Willem Van Onsem
    2 days ago










  • Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
    – LMHull
    2 days ago












  • @LMHull edit the expected output into your question, please. Don't post it as a comment.
    – timgeb
    2 days ago






  • 1




    There is a duplicated key on the users dictionary '196'.
    – Daniel Mesejo
    2 days ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











users = {'196': ('110', '1'), '186': ('269', '1'), '22': ('68', '4'), '196': ('650', '3')}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)', '68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}


My code to create a nested dictionary with keys a unique list of users (keys from users) but using keys from movies to replace values of movies IDs (first item of value list from users) and keeping scores (second item of value list from users) is:




users_preference = {k: list(set().union((*map(lambda x: [x for x in movies.values()], v[0])) )) for k, v in users.items() }




But this returns all movies for each user, and I don't know how to add scores to this. Could you please help? Thank you.



The expected output is similar to:



users_preference = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}









share|improve this question















users = {'196': ('110', '1'), '186': ('269', '1'), '22': ('68', '4'), '196': ('650', '3')}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)', '68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}


My code to create a nested dictionary with keys a unique list of users (keys from users) but using keys from movies to replace values of movies IDs (first item of value list from users) and keeping scores (second item of value list from users) is:




users_preference = {k: list(set().union((*map(lambda x: [x for x in movies.values()], v[0])) )) for k, v in users.items() }




But this returns all movies for each user, and I don't know how to add scores to this. Could you please help? Thank you.



The expected output is similar to:



users_preference = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}






python dictionary nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









timgeb

43k105883




43k105883










asked 2 days ago









LMHull

426




426












  • It would be very helpful if you posted your expected output instead of just describing it in words.
    – timgeb
    2 days ago










  • Why do these users have a 2-tuple with '1'?
    – Willem Van Onsem
    2 days ago










  • Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
    – LMHull
    2 days ago












  • @LMHull edit the expected output into your question, please. Don't post it as a comment.
    – timgeb
    2 days ago






  • 1




    There is a duplicated key on the users dictionary '196'.
    – Daniel Mesejo
    2 days ago


















  • It would be very helpful if you posted your expected output instead of just describing it in words.
    – timgeb
    2 days ago










  • Why do these users have a 2-tuple with '1'?
    – Willem Van Onsem
    2 days ago










  • Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
    – LMHull
    2 days ago












  • @LMHull edit the expected output into your question, please. Don't post it as a comment.
    – timgeb
    2 days ago






  • 1




    There is a duplicated key on the users dictionary '196'.
    – Daniel Mesejo
    2 days ago
















It would be very helpful if you posted your expected output instead of just describing it in words.
– timgeb
2 days ago




It would be very helpful if you posted your expected output instead of just describing it in words.
– timgeb
2 days ago












Why do these users have a 2-tuple with '1'?
– Willem Van Onsem
2 days ago




Why do these users have a 2-tuple with '1'?
– Willem Van Onsem
2 days ago












Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
– LMHull
2 days ago






Sorry yes the expected output: `Dct3 = {'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}
– LMHull
2 days ago














@LMHull edit the expected output into your question, please. Don't post it as a comment.
– timgeb
2 days ago




@LMHull edit the expected output into your question, please. Don't post it as a comment.
– timgeb
2 days ago




1




1




There is a duplicated key on the users dictionary '196'.
– Daniel Mesejo
2 days ago




There is a duplicated key on the users dictionary '196'.
– Daniel Mesejo
2 days ago












4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










The expected output cannot be achieved using the format of users, I suggest you change to a dictionary where the values are a list of tuples:



users = {'196': [('110', '1'), ('650', '3')], '186': [('269', '1')], '22': [('68', '4')]}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)',
'68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}

user_preference = {user: {movies.get(movie, movie): rank for movie, rank in preferences} for user, preferences in
users.items()}

print(user_preference)


Output



{'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}, '22': {'Crow, The (1994)': '4'}, '186': {'Full Monty, The (1997)': '1'}}





share|improve this answer





















  • You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
    – LMHull
    2 days ago


















up vote
1
down vote













Dct3 = {k:(movies[v[0]],v[1]) for k, v in users.items()}


Keep it simple, you can use keys to access values from movies dict.
Just think of it as creating a new tuple as values for each key in users.






share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
    – LMHull
    2 days ago










  • No worries at all, welcome to the world of programming :) @LMHull
    – Paritosh Singh
    2 days ago


















up vote
1
down vote













More verbose



Dct3 = {}
for k, v in users.items():
v = list(v)
v[0] = movies[v[0]]
Dct3[k] = v

print (Dct3)
#=> {'196': ['Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'], '186': ['Full Monty, The (1997)', '1'], '22': ['Crow, The (1994)', '4']}





share|improve this answer

















  • 1




    Thank you very much. I really appreciate your help.
    – LMHull
    2 days ago


















up vote
0
down vote













You can issue



{user:(movies[movie_id], score) for user, (movie_id, score) in users.items()}


which produces



{'186': ('Full Monty, The (1997)', '1'),
'196': ('Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'),
'22': ('Crow, The (1994)', '4')}


Note that there is a duplicate key in your dictionary users ('196'). (Pointed out by Daniel Mesejo in the comments.) Dictionary keys must be unique, so users actually looks like this:



>>> users
>>> {'186': ('269', '1'), '196': ('650', '3'), '22': ('68', '4')}


~edit~



Traditional for loop that does the same.



result = {}
for user, (movie_id, score) in users.items():
result[user] = (movies[movie_id], score)





share|improve this answer























  • Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
    – LMHull
    2 days ago










  • @LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
    – timgeb
    2 days ago












  • I will do, thanks for your advice.
    – LMHull
    2 days ago











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239262%2fnested-dictionary-from-dict1-and-dict2-using-keys-from-dict1-and-values-from-dic%23new-answer', 'question_page');
}
);

Post as a guest
































4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










The expected output cannot be achieved using the format of users, I suggest you change to a dictionary where the values are a list of tuples:



users = {'196': [('110', '1'), ('650', '3')], '186': [('269', '1')], '22': [('68', '4')]}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)',
'68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}

user_preference = {user: {movies.get(movie, movie): rank for movie, rank in preferences} for user, preferences in
users.items()}

print(user_preference)


Output



{'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}, '22': {'Crow, The (1994)': '4'}, '186': {'Full Monty, The (1997)': '1'}}





share|improve this answer





















  • You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
    – LMHull
    2 days ago















up vote
0
down vote



accepted










The expected output cannot be achieved using the format of users, I suggest you change to a dictionary where the values are a list of tuples:



users = {'196': [('110', '1'), ('650', '3')], '186': [('269', '1')], '22': [('68', '4')]}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)',
'68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}

user_preference = {user: {movies.get(movie, movie): rank for movie, rank in preferences} for user, preferences in
users.items()}

print(user_preference)


Output



{'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}, '22': {'Crow, The (1994)': '4'}, '186': {'Full Monty, The (1997)': '1'}}





share|improve this answer





















  • You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
    – LMHull
    2 days ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






The expected output cannot be achieved using the format of users, I suggest you change to a dictionary where the values are a list of tuples:



users = {'196': [('110', '1'), ('650', '3')], '186': [('269', '1')], '22': [('68', '4')]}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)',
'68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}

user_preference = {user: {movies.get(movie, movie): rank for movie, rank in preferences} for user, preferences in
users.items()}

print(user_preference)


Output



{'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}, '22': {'Crow, The (1994)': '4'}, '186': {'Full Monty, The (1997)': '1'}}





share|improve this answer












The expected output cannot be achieved using the format of users, I suggest you change to a dictionary where the values are a list of tuples:



users = {'196': [('110', '1'), ('650', '3')], '186': [('269', '1')], '22': [('68', '4')]}
movies = {'110': 'Operation Dumbo Drop (1995)', '186': 'Blues Brothers, The (1980)', '269': 'Full Monty, The (1997)',
'68': 'Crow, The (1994)', '650': 'Seventh Seal, The (Sjunde inseglet, Det) (1957)'}

user_preference = {user: {movies.get(movie, movie): rank for movie, rank in preferences} for user, preferences in
users.items()}

print(user_preference)


Output



{'196': {'Operation Dumbo Drop (1995)': '1', 'Seventh Seal, The (Sjunde inseglet, Det) (1957)': '3'}, '22': {'Crow, The (1994)': '4'}, '186': {'Full Monty, The (1997)': '1'}}






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Daniel Mesejo

7,6341821




7,6341821












  • You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
    – LMHull
    2 days ago


















  • You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
    – LMHull
    2 days ago
















You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
– LMHull
2 days ago




You are actually so right about this. I have got the wrong dictionary users! I am iterating through a data file to get users and so far I have picked up just one movie per user, instead of list of tuples as you very correctly suggested. Thanks for your attention to detail.
– LMHull
2 days ago












up vote
1
down vote













Dct3 = {k:(movies[v[0]],v[1]) for k, v in users.items()}


Keep it simple, you can use keys to access values from movies dict.
Just think of it as creating a new tuple as values for each key in users.






share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
    – LMHull
    2 days ago










  • No worries at all, welcome to the world of programming :) @LMHull
    – Paritosh Singh
    2 days ago















up vote
1
down vote













Dct3 = {k:(movies[v[0]],v[1]) for k, v in users.items()}


Keep it simple, you can use keys to access values from movies dict.
Just think of it as creating a new tuple as values for each key in users.






share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
    – LMHull
    2 days ago










  • No worries at all, welcome to the world of programming :) @LMHull
    – Paritosh Singh
    2 days ago













up vote
1
down vote










up vote
1
down vote









Dct3 = {k:(movies[v[0]],v[1]) for k, v in users.items()}


Keep it simple, you can use keys to access values from movies dict.
Just think of it as creating a new tuple as values for each key in users.






share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









Dct3 = {k:(movies[v[0]],v[1]) for k, v in users.items()}


Keep it simple, you can use keys to access values from movies dict.
Just think of it as creating a new tuple as values for each key in users.







share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 2 days ago









Paritosh Singh

1665




1665




New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
    – LMHull
    2 days ago










  • No worries at all, welcome to the world of programming :) @LMHull
    – Paritosh Singh
    2 days ago


















  • Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
    – LMHull
    2 days ago










  • No worries at all, welcome to the world of programming :) @LMHull
    – Paritosh Singh
    2 days ago
















Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
– LMHull
2 days ago




Thank you very much, really appreciate it. I feel so much better know after so many hours of trying this.
– LMHull
2 days ago












No worries at all, welcome to the world of programming :) @LMHull
– Paritosh Singh
2 days ago




No worries at all, welcome to the world of programming :) @LMHull
– Paritosh Singh
2 days ago










up vote
1
down vote













More verbose



Dct3 = {}
for k, v in users.items():
v = list(v)
v[0] = movies[v[0]]
Dct3[k] = v

print (Dct3)
#=> {'196': ['Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'], '186': ['Full Monty, The (1997)', '1'], '22': ['Crow, The (1994)', '4']}





share|improve this answer

















  • 1




    Thank you very much. I really appreciate your help.
    – LMHull
    2 days ago















up vote
1
down vote













More verbose



Dct3 = {}
for k, v in users.items():
v = list(v)
v[0] = movies[v[0]]
Dct3[k] = v

print (Dct3)
#=> {'196': ['Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'], '186': ['Full Monty, The (1997)', '1'], '22': ['Crow, The (1994)', '4']}





share|improve this answer

















  • 1




    Thank you very much. I really appreciate your help.
    – LMHull
    2 days ago













up vote
1
down vote










up vote
1
down vote









More verbose



Dct3 = {}
for k, v in users.items():
v = list(v)
v[0] = movies[v[0]]
Dct3[k] = v

print (Dct3)
#=> {'196': ['Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'], '186': ['Full Monty, The (1997)', '1'], '22': ['Crow, The (1994)', '4']}





share|improve this answer












More verbose



Dct3 = {}
for k, v in users.items():
v = list(v)
v[0] = movies[v[0]]
Dct3[k] = v

print (Dct3)
#=> {'196': ['Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'], '186': ['Full Monty, The (1997)', '1'], '22': ['Crow, The (1994)', '4']}






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









iGian

2,2832620




2,2832620








  • 1




    Thank you very much. I really appreciate your help.
    – LMHull
    2 days ago














  • 1




    Thank you very much. I really appreciate your help.
    – LMHull
    2 days ago








1




1




Thank you very much. I really appreciate your help.
– LMHull
2 days ago




Thank you very much. I really appreciate your help.
– LMHull
2 days ago










up vote
0
down vote













You can issue



{user:(movies[movie_id], score) for user, (movie_id, score) in users.items()}


which produces



{'186': ('Full Monty, The (1997)', '1'),
'196': ('Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'),
'22': ('Crow, The (1994)', '4')}


Note that there is a duplicate key in your dictionary users ('196'). (Pointed out by Daniel Mesejo in the comments.) Dictionary keys must be unique, so users actually looks like this:



>>> users
>>> {'186': ('269', '1'), '196': ('650', '3'), '22': ('68', '4')}


~edit~



Traditional for loop that does the same.



result = {}
for user, (movie_id, score) in users.items():
result[user] = (movies[movie_id], score)





share|improve this answer























  • Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
    – LMHull
    2 days ago










  • @LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
    – timgeb
    2 days ago












  • I will do, thanks for your advice.
    – LMHull
    2 days ago















up vote
0
down vote













You can issue



{user:(movies[movie_id], score) for user, (movie_id, score) in users.items()}


which produces



{'186': ('Full Monty, The (1997)', '1'),
'196': ('Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'),
'22': ('Crow, The (1994)', '4')}


Note that there is a duplicate key in your dictionary users ('196'). (Pointed out by Daniel Mesejo in the comments.) Dictionary keys must be unique, so users actually looks like this:



>>> users
>>> {'186': ('269', '1'), '196': ('650', '3'), '22': ('68', '4')}


~edit~



Traditional for loop that does the same.



result = {}
for user, (movie_id, score) in users.items():
result[user] = (movies[movie_id], score)





share|improve this answer























  • Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
    – LMHull
    2 days ago










  • @LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
    – timgeb
    2 days ago












  • I will do, thanks for your advice.
    – LMHull
    2 days ago













up vote
0
down vote










up vote
0
down vote









You can issue



{user:(movies[movie_id], score) for user, (movie_id, score) in users.items()}


which produces



{'186': ('Full Monty, The (1997)', '1'),
'196': ('Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'),
'22': ('Crow, The (1994)', '4')}


Note that there is a duplicate key in your dictionary users ('196'). (Pointed out by Daniel Mesejo in the comments.) Dictionary keys must be unique, so users actually looks like this:



>>> users
>>> {'186': ('269', '1'), '196': ('650', '3'), '22': ('68', '4')}


~edit~



Traditional for loop that does the same.



result = {}
for user, (movie_id, score) in users.items():
result[user] = (movies[movie_id], score)





share|improve this answer














You can issue



{user:(movies[movie_id], score) for user, (movie_id, score) in users.items()}


which produces



{'186': ('Full Monty, The (1997)', '1'),
'196': ('Seventh Seal, The (Sjunde inseglet, Det) (1957)', '3'),
'22': ('Crow, The (1994)', '4')}


Note that there is a duplicate key in your dictionary users ('196'). (Pointed out by Daniel Mesejo in the comments.) Dictionary keys must be unique, so users actually looks like this:



>>> users
>>> {'186': ('269', '1'), '196': ('650', '3'), '22': ('68', '4')}


~edit~



Traditional for loop that does the same.



result = {}
for user, (movie_id, score) in users.items():
result[user] = (movies[movie_id], score)






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









timgeb

43k105883




43k105883












  • Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
    – LMHull
    2 days ago










  • @LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
    – timgeb
    2 days ago












  • I will do, thanks for your advice.
    – LMHull
    2 days ago


















  • Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
    – LMHull
    2 days ago










  • @LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
    – timgeb
    2 days ago












  • I will do, thanks for your advice.
    – LMHull
    2 days ago
















Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
– LMHull
2 days ago




Excellent, thank you so much. I am learning python so I didn't know that keys must be unique. Very happy now, embarrassingly I tried to do this for about 5 hours...
– LMHull
2 days ago












@LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
– timgeb
2 days ago






@LMHull If you are new to Python, my tip is to stick to traditional for loops before writing oneliners. Once you figured out how the different data structures operate, you can venture into list-, dict- and set-comprehensions.
– timgeb
2 days ago














I will do, thanks for your advice.
– LMHull
2 days ago




I will do, thanks for your advice.
– LMHull
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239262%2fnested-dictionary-from-dict1-and-dict2-using-keys-from-dict1-and-values-from-dic%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Bressuire

Vorschmack

Quarantine