python 3 adding multiple values under same key in dictionary [duplicate]
This question already has an answer here:
append multiple values for one key in a dictionary [duplicate]
7 answers
i have a problem with my code, tried looking up how to fix it and tried multiple methods but its just not going
Here is what i got:
with open("people-data.txt") as f:
children= {}
for line in f.readlines():
parent, child = line.split("->")
children[parent] = child
I tried using: children[parent].append(child)
and other things.
My file looks like this:
Mary->Patricia
Mary->Lisa
Patricia->Barbara
What I would like is when I use children[Mary]
I get ['Patricia', 'Lisa']
but what my code does is give my just 'Lisa'
and overrides 'Patricia'
.
python python-3.x dictionary
marked as duplicate by DeepSpace
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 16:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
append multiple values for one key in a dictionary [duplicate]
7 answers
i have a problem with my code, tried looking up how to fix it and tried multiple methods but its just not going
Here is what i got:
with open("people-data.txt") as f:
children= {}
for line in f.readlines():
parent, child = line.split("->")
children[parent] = child
I tried using: children[parent].append(child)
and other things.
My file looks like this:
Mary->Patricia
Mary->Lisa
Patricia->Barbara
What I would like is when I use children[Mary]
I get ['Patricia', 'Lisa']
but what my code does is give my just 'Lisa'
and overrides 'Patricia'
.
python python-3.x dictionary
marked as duplicate by DeepSpace
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 16:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It should bechildren[parent] = child
. In your case you would store the key"parent"
and overwrite it.
– Anton vBR
Nov 25 '18 at 16:22
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
Of course it overwrites. What else wouldchildren[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by usingchildren = defaultdict(list)
andchildren[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24
add a comment |
This question already has an answer here:
append multiple values for one key in a dictionary [duplicate]
7 answers
i have a problem with my code, tried looking up how to fix it and tried multiple methods but its just not going
Here is what i got:
with open("people-data.txt") as f:
children= {}
for line in f.readlines():
parent, child = line.split("->")
children[parent] = child
I tried using: children[parent].append(child)
and other things.
My file looks like this:
Mary->Patricia
Mary->Lisa
Patricia->Barbara
What I would like is when I use children[Mary]
I get ['Patricia', 'Lisa']
but what my code does is give my just 'Lisa'
and overrides 'Patricia'
.
python python-3.x dictionary
This question already has an answer here:
append multiple values for one key in a dictionary [duplicate]
7 answers
i have a problem with my code, tried looking up how to fix it and tried multiple methods but its just not going
Here is what i got:
with open("people-data.txt") as f:
children= {}
for line in f.readlines():
parent, child = line.split("->")
children[parent] = child
I tried using: children[parent].append(child)
and other things.
My file looks like this:
Mary->Patricia
Mary->Lisa
Patricia->Barbara
What I would like is when I use children[Mary]
I get ['Patricia', 'Lisa']
but what my code does is give my just 'Lisa'
and overrides 'Patricia'
.
This question already has an answer here:
append multiple values for one key in a dictionary [duplicate]
7 answers
python python-3.x dictionary
python python-3.x dictionary
edited Nov 25 '18 at 16:31
timgeb
50.7k116393
50.7k116393
asked Nov 25 '18 at 16:21
D. K.D. K.
455
455
marked as duplicate by DeepSpace
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 16:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by DeepSpace
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 16:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It should bechildren[parent] = child
. In your case you would store the key"parent"
and overwrite it.
– Anton vBR
Nov 25 '18 at 16:22
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
Of course it overwrites. What else wouldchildren[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by usingchildren = defaultdict(list)
andchildren[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24
add a comment |
It should bechildren[parent] = child
. In your case you would store the key"parent"
and overwrite it.
– Anton vBR
Nov 25 '18 at 16:22
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
Of course it overwrites. What else wouldchildren[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by usingchildren = defaultdict(list)
andchildren[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24
It should be
children[parent] = child
. In your case you would store the key "parent"
and overwrite it.– Anton vBR
Nov 25 '18 at 16:22
It should be
children[parent] = child
. In your case you would store the key "parent"
and overwrite it.– Anton vBR
Nov 25 '18 at 16:22
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
Of course it overwrites. What else would
children[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by using children = defaultdict(list)
and children[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24
Of course it overwrites. What else would
children[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by using children = defaultdict(list)
and children[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24
add a comment |
1 Answer
1
active
oldest
votes
I tried using:
children[parent].append(child)
That will work as long as you are using lists for your dictionary-values.
The easiest fix to achieve that would be to make children a defaultdict
, i.e.
from collections import defaultdict
children = defaultdict(list)
and then use
children[parent].append(child)
in your code.
Demo:
>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})
Yes, and don't forget:children[parent].append(child)
for a complete solution.
– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
@DávidKačmarchildren['Mary']
.
– timgeb
Nov 25 '18 at 16:28
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I tried using:
children[parent].append(child)
That will work as long as you are using lists for your dictionary-values.
The easiest fix to achieve that would be to make children a defaultdict
, i.e.
from collections import defaultdict
children = defaultdict(list)
and then use
children[parent].append(child)
in your code.
Demo:
>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})
Yes, and don't forget:children[parent].append(child)
for a complete solution.
– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
@DávidKačmarchildren['Mary']
.
– timgeb
Nov 25 '18 at 16:28
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
add a comment |
I tried using:
children[parent].append(child)
That will work as long as you are using lists for your dictionary-values.
The easiest fix to achieve that would be to make children a defaultdict
, i.e.
from collections import defaultdict
children = defaultdict(list)
and then use
children[parent].append(child)
in your code.
Demo:
>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})
Yes, and don't forget:children[parent].append(child)
for a complete solution.
– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
@DávidKačmarchildren['Mary']
.
– timgeb
Nov 25 '18 at 16:28
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
add a comment |
I tried using:
children[parent].append(child)
That will work as long as you are using lists for your dictionary-values.
The easiest fix to achieve that would be to make children a defaultdict
, i.e.
from collections import defaultdict
children = defaultdict(list)
and then use
children[parent].append(child)
in your code.
Demo:
>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})
I tried using:
children[parent].append(child)
That will work as long as you are using lists for your dictionary-values.
The easiest fix to achieve that would be to make children a defaultdict
, i.e.
from collections import defaultdict
children = defaultdict(list)
and then use
children[parent].append(child)
in your code.
Demo:
>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})
edited Nov 25 '18 at 16:32
answered Nov 25 '18 at 16:23
timgebtimgeb
50.7k116393
50.7k116393
Yes, and don't forget:children[parent].append(child)
for a complete solution.
– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
@DávidKačmarchildren['Mary']
.
– timgeb
Nov 25 '18 at 16:28
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
add a comment |
Yes, and don't forget:children[parent].append(child)
for a complete solution.
– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
@DávidKačmarchildren['Mary']
.
– timgeb
Nov 25 '18 at 16:28
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
Yes, and don't forget:
children[parent].append(child)
for a complete solution.– Anton vBR
Nov 25 '18 at 16:25
Yes, and don't forget:
children[parent].append(child)
for a complete solution.– Anton vBR
Nov 25 '18 at 16:25
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
Thank you, it is now creating what i wanted, but the problem is that when i call children("Mary") i get that it is not callable, i need the values to work with later on
– D. K.
Nov 25 '18 at 16:27
1
1
@DávidKačmar
children['Mary']
.– timgeb
Nov 25 '18 at 16:28
@DávidKačmar
children['Mary']
.– timgeb
Nov 25 '18 at 16:28
1
1
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
Thank you!!!! i love you guys!! or girls :D saved my day :D thanks alot
– D. K.
Nov 25 '18 at 16:29
add a comment |
It should be
children[parent] = child
. In your case you would store the key"parent"
and overwrite it.– Anton vBR
Nov 25 '18 at 16:22
ye sorry, my mistake from copying, let my correct it
– D. K.
Nov 25 '18 at 16:23
You need to use a defaultdict from collections.
– Anton vBR
Nov 25 '18 at 16:24
Of course it overwrites. What else would
children[parent] = child
do? You need to keep lists in each value of the dict, which can be easily done by usingchildren = defaultdict(list)
andchildren[parent].append(child)
– DeepSpace
Nov 25 '18 at 16:24