How to add multiple values to a single key
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
In my code I have generated two lists, t_fail and p_fail. For these two lists the index number relates both p_fail and t_fail. So for the 100 in t_fail, the 100 in p_fail corresponds to it.
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
I want to make a dictionary that has the t_fail values as keys and the p_fail values as the values of the dictionary. But if the same t_fail value repeats itself, I want to be able to append to the corresponding p_fail value to the dictionary key.
for example:
dict = {
100: [100,105]
200: [105]
300: [110]
}
I was thinking about using some sort of for loop but other than that, I'm a little lost.
Thank you
python-3.x
add a comment |
In my code I have generated two lists, t_fail and p_fail. For these two lists the index number relates both p_fail and t_fail. So for the 100 in t_fail, the 100 in p_fail corresponds to it.
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
I want to make a dictionary that has the t_fail values as keys and the p_fail values as the values of the dictionary. But if the same t_fail value repeats itself, I want to be able to append to the corresponding p_fail value to the dictionary key.
for example:
dict = {
100: [100,105]
200: [105]
300: [110]
}
I was thinking about using some sort of for loop but other than that, I'm a little lost.
Thank you
python-3.x
add a comment |
In my code I have generated two lists, t_fail and p_fail. For these two lists the index number relates both p_fail and t_fail. So for the 100 in t_fail, the 100 in p_fail corresponds to it.
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
I want to make a dictionary that has the t_fail values as keys and the p_fail values as the values of the dictionary. But if the same t_fail value repeats itself, I want to be able to append to the corresponding p_fail value to the dictionary key.
for example:
dict = {
100: [100,105]
200: [105]
300: [110]
}
I was thinking about using some sort of for loop but other than that, I'm a little lost.
Thank you
python-3.x
In my code I have generated two lists, t_fail and p_fail. For these two lists the index number relates both p_fail and t_fail. So for the 100 in t_fail, the 100 in p_fail corresponds to it.
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
I want to make a dictionary that has the t_fail values as keys and the p_fail values as the values of the dictionary. But if the same t_fail value repeats itself, I want to be able to append to the corresponding p_fail value to the dictionary key.
for example:
dict = {
100: [100,105]
200: [105]
300: [110]
}
I was thinking about using some sort of for loop but other than that, I'm a little lost.
Thank you
python-3.x
python-3.x
asked Jan 4 at 16:04
bbalzanobbalzano
185
185
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
As mentionned @Marcus, this is a good use case for defaultdict
from collections import defaultdict
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
d = defaultdict(list)
for t, p in zip(t_fail, p_fail):
d[t].append(p)
print(d)
# defaultdict(<class 'list'>, {100: [100, 105], 200: [105], 300: [110]})
Two ways to pretty print your dict
First by looping over the items
for k, v in sorted(d.items()):
print(k,v)
The second, by unpacking the value and specifying the separator
print(*sorted(d.items()), sep='n')
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
add a comment |
You can try like this.
As dictionary is an unordered data type so if you are looking for ordered keys, you can use OrderedDict from collections module.
d = {} # d = dict()
for key, value in zip(t_fail, p_fail):
if key in d:
d[key].append(value)
else:
d[key] = [value]
print(d) # {200: [105], 300: [110], 100: [100, 105]}
Pretty printing the dictionary.
import json
s = json.dumps(d, indent=4)
print(s)
# {
# "200": [
# 105
# ],
# "300": [
# 110
# ],
# "100": [
# 100,
# 105
# ]
# }
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
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%2f54042388%2fhow-to-add-multiple-values-to-a-single-key%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
As mentionned @Marcus, this is a good use case for defaultdict
from collections import defaultdict
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
d = defaultdict(list)
for t, p in zip(t_fail, p_fail):
d[t].append(p)
print(d)
# defaultdict(<class 'list'>, {100: [100, 105], 200: [105], 300: [110]})
Two ways to pretty print your dict
First by looping over the items
for k, v in sorted(d.items()):
print(k,v)
The second, by unpacking the value and specifying the separator
print(*sorted(d.items()), sep='n')
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
add a comment |
As mentionned @Marcus, this is a good use case for defaultdict
from collections import defaultdict
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
d = defaultdict(list)
for t, p in zip(t_fail, p_fail):
d[t].append(p)
print(d)
# defaultdict(<class 'list'>, {100: [100, 105], 200: [105], 300: [110]})
Two ways to pretty print your dict
First by looping over the items
for k, v in sorted(d.items()):
print(k,v)
The second, by unpacking the value and specifying the separator
print(*sorted(d.items()), sep='n')
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
add a comment |
As mentionned @Marcus, this is a good use case for defaultdict
from collections import defaultdict
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
d = defaultdict(list)
for t, p in zip(t_fail, p_fail):
d[t].append(p)
print(d)
# defaultdict(<class 'list'>, {100: [100, 105], 200: [105], 300: [110]})
Two ways to pretty print your dict
First by looping over the items
for k, v in sorted(d.items()):
print(k,v)
The second, by unpacking the value and specifying the separator
print(*sorted(d.items()), sep='n')
As mentionned @Marcus, this is a good use case for defaultdict
from collections import defaultdict
t_fail = [100,100,200,300]
p_fail = [100,105,105,110]
d = defaultdict(list)
for t, p in zip(t_fail, p_fail):
d[t].append(p)
print(d)
# defaultdict(<class 'list'>, {100: [100, 105], 200: [105], 300: [110]})
Two ways to pretty print your dict
First by looping over the items
for k, v in sorted(d.items()):
print(k,v)
The second, by unpacking the value and specifying the separator
print(*sorted(d.items()), sep='n')
edited Jan 4 at 17:17
answered Jan 4 at 16:29
BlueSheepTokenBlueSheepToken
2,1691617
2,1691617
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
add a comment |
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
Is it possible for it to print without the "defaultdict(<class 'list'>," section and have each key value pair on it's own line?
– bbalzano
Jan 4 at 17:11
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
@bbalzano I edited my answer, to fillful your needs
– BlueSheepToken
Jan 4 at 17:17
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
Is there any way to save the sorted version of the dictionary to a text file? The only way I have been able to save it to a text file is to save the original version
– bbalzano
Jan 4 at 19:57
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You can dump it into a json file
– BlueSheepToken
Jan 4 at 19:58
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
You might want to open a new question for thius :)
– BlueSheepToken
Jan 4 at 19:59
add a comment |
You can try like this.
As dictionary is an unordered data type so if you are looking for ordered keys, you can use OrderedDict from collections module.
d = {} # d = dict()
for key, value in zip(t_fail, p_fail):
if key in d:
d[key].append(value)
else:
d[key] = [value]
print(d) # {200: [105], 300: [110], 100: [100, 105]}
Pretty printing the dictionary.
import json
s = json.dumps(d, indent=4)
print(s)
# {
# "200": [
# 105
# ],
# "300": [
# 110
# ],
# "100": [
# 100,
# 105
# ]
# }
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
add a comment |
You can try like this.
As dictionary is an unordered data type so if you are looking for ordered keys, you can use OrderedDict from collections module.
d = {} # d = dict()
for key, value in zip(t_fail, p_fail):
if key in d:
d[key].append(value)
else:
d[key] = [value]
print(d) # {200: [105], 300: [110], 100: [100, 105]}
Pretty printing the dictionary.
import json
s = json.dumps(d, indent=4)
print(s)
# {
# "200": [
# 105
# ],
# "300": [
# 110
# ],
# "100": [
# 100,
# 105
# ]
# }
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
add a comment |
You can try like this.
As dictionary is an unordered data type so if you are looking for ordered keys, you can use OrderedDict from collections module.
d = {} # d = dict()
for key, value in zip(t_fail, p_fail):
if key in d:
d[key].append(value)
else:
d[key] = [value]
print(d) # {200: [105], 300: [110], 100: [100, 105]}
Pretty printing the dictionary.
import json
s = json.dumps(d, indent=4)
print(s)
# {
# "200": [
# 105
# ],
# "300": [
# 110
# ],
# "100": [
# 100,
# 105
# ]
# }
You can try like this.
As dictionary is an unordered data type so if you are looking for ordered keys, you can use OrderedDict from collections module.
d = {} # d = dict()
for key, value in zip(t_fail, p_fail):
if key in d:
d[key].append(value)
else:
d[key] = [value]
print(d) # {200: [105], 300: [110], 100: [100, 105]}
Pretty printing the dictionary.
import json
s = json.dumps(d, indent=4)
print(s)
# {
# "200": [
# 105
# ],
# "300": [
# 110
# ],
# "100": [
# 100,
# 105
# ]
# }
edited Jan 4 at 16:23
answered Jan 4 at 16:13
hygullhygull
4,05921632
4,05921632
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
add a comment |
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
This is a good use case for a defaultdict. The example in the documentation shows how it is used with lists: docs.python.org/3.7/library/…
– Marcus
Jan 4 at 16:16
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
Yeah, that is excellent. Thank you. I know it, it is just to use the concept of dictionary instead of using the already implemented feature.
– hygull
Jan 4 at 16:18
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
What's the relevance of OrderedDict here?
– glibdud
Jan 4 at 16:20
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
I do not see any use but the problem has o/p with keys in sequence as they appear in the list. So, I thought, user will be looking for keys in order also and that forced me to update.
– hygull
Jan 4 at 16:25
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%2f54042388%2fhow-to-add-multiple-values-to-a-single-key%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