I want to create function that keep appending new data to nested dictionary“Least Astonishment” and the Mutable Default ArgumentHow can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?How can I safely create a nested directory?Add new keys to a dictionary?Create a dictionary with list comprehension in PythonCreating a dictionary from a csv file?“Large data” work flows using pandasappend lists in dictionary depending on another list python?creating n nested loops from a dictionary of listsInsert key value to nested dictionaries in list by indexConvert pandas multiindex dataframe to nested dictionarycreating lists from nested dictionaries
Is there any effect in D&D 5e that cannot be undone?
How can a flywheel makes engine runs smoothly?
Can you cover a cube with copies of this shape?
Have Steve Rogers (Captain America) and a young Erik Lehnsherr (Magneto) interacted during WWII?
Does knowing the surface area of all faces uniquely determine a tetrahedron?
How to ask if I can mow my neighbor's lawn
Is it possible for underground bunkers on different continents to be connected?
How can I ping multiple IP addresses at the same time?
Why do you need to heat the pan before heating the olive oil?
Will users know a CardView is clickable?
Lead the way to this Literary Knight to its final “DESTINATION”
What is the precise meaning of "подсел на мак"?
New Site Design!
How to make a villain when your PCs are villains?
Explicit direct #include vs. Non-contractual transitive #include
How do I become a better writer when I hate reading?
Time at 1G acceleration to travel 100000 light years
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
2 Managed Packages in 1 Dev Org
SQL Server has encountered occurences of I/O requests taking longer than 15 seconds
What do I put on my resume to make the company i'm applying to think i'm mature enough to handle a job?
Basic power tool set for Home repair and simple projects
How can I detect if I'm in a subshell?
Co-worker is now managing my team. Does this mean that I'm being demoted?
I want to create function that keep appending new data to nested dictionary
“Least Astonishment” and the Mutable Default ArgumentHow can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?How can I safely create a nested directory?Add new keys to a dictionary?Create a dictionary with list comprehension in PythonCreating a dictionary from a csv file?“Large data” work flows using pandasappend lists in dictionary depending on another list python?creating n nested loops from a dictionary of listsInsert key value to nested dictionaries in list by indexConvert pandas multiindex dataframe to nested dictionarycreating lists from nested dictionaries
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to store new data(Key, value) to the nested dictionary.
But I don't have any clue how to fix it.
def add(a,b,c,d,container)
container = # as database
data =
data[a] = ["first": b, "second": c, "third": d]
for e in data:
if date[date] not in calendar:
container[date[a]] =
container[date[a]].update(["first": b, "second": c, "third": d)
add(name, 1, 2, hi, container)
add(name1, 2, 1, hi, container)
I see the following output:
name: ["first": 1, "second": 2, "third":hi ]
name1: ["first": 2, "second": 1, "third":hi ]
I expect the output as:
name: ["first": 1, "second": 2, "third":hi ], name1: ["first": 2, "second": 1, "third":hi ]
Please help me out!
python
add a comment |
I want to store new data(Key, value) to the nested dictionary.
But I don't have any clue how to fix it.
def add(a,b,c,d,container)
container = # as database
data =
data[a] = ["first": b, "second": c, "third": d]
for e in data:
if date[date] not in calendar:
container[date[a]] =
container[date[a]].update(["first": b, "second": c, "third": d)
add(name, 1, 2, hi, container)
add(name1, 2, 1, hi, container)
I see the following output:
name: ["first": 1, "second": 2, "third":hi ]
name1: ["first": 2, "second": 1, "third":hi ]
I expect the output as:
name: ["first": 1, "second": 2, "third":hi ], name1: ["first": 2, "second": 1, "third":hi ]
Please help me out!
python
What's yourfunction
? Did you meanadd
?
– knh190
Mar 25 at 4:01
add a comment |
I want to store new data(Key, value) to the nested dictionary.
But I don't have any clue how to fix it.
def add(a,b,c,d,container)
container = # as database
data =
data[a] = ["first": b, "second": c, "third": d]
for e in data:
if date[date] not in calendar:
container[date[a]] =
container[date[a]].update(["first": b, "second": c, "third": d)
add(name, 1, 2, hi, container)
add(name1, 2, 1, hi, container)
I see the following output:
name: ["first": 1, "second": 2, "third":hi ]
name1: ["first": 2, "second": 1, "third":hi ]
I expect the output as:
name: ["first": 1, "second": 2, "third":hi ], name1: ["first": 2, "second": 1, "third":hi ]
Please help me out!
python
I want to store new data(Key, value) to the nested dictionary.
But I don't have any clue how to fix it.
def add(a,b,c,d,container)
container = # as database
data =
data[a] = ["first": b, "second": c, "third": d]
for e in data:
if date[date] not in calendar:
container[date[a]] =
container[date[a]].update(["first": b, "second": c, "third": d)
add(name, 1, 2, hi, container)
add(name1, 2, 1, hi, container)
I see the following output:
name: ["first": 1, "second": 2, "third":hi ]
name1: ["first": 2, "second": 1, "third":hi ]
I expect the output as:
name: ["first": 1, "second": 2, "third":hi ], name1: ["first": 2, "second": 1, "third":hi ]
Please help me out!
python
python
edited Mar 25 at 4:18
Neal Lee
asked Mar 25 at 3:51
Neal LeeNeal Lee
32
32
What's yourfunction
? Did you meanadd
?
– knh190
Mar 25 at 4:01
add a comment |
What's yourfunction
? Did you meanadd
?
– knh190
Mar 25 at 4:01
What's your
function
? Did you mean add
?– knh190
Mar 25 at 4:01
What's your
function
? Did you mean add
?– knh190
Mar 25 at 4:01
add a comment |
3 Answers
3
active
oldest
votes
i am creating a global_dict which is main dictionary where storing of all the
dict element is taken place.
for your given problem, if you want to add the data in a dict continously and that dict have same structure, then better:
is to create a default dict , and in that dict update the values and finally add that new small dict to the main dict using update method.
def funcion(a,b,c,d, container):
new_dic=a:['first':b,"Second":c,"third":d]
container.update(new_dic)
funcion('name', 1, 2, 'hi')
funcion('name1', 2, 1,'hi')
print(container)
"""
output
'name': ['first': 1, 'Second': 2, 'third': 'hi'],
'name1': ['first': 2, 'Second': 1, 'third': 'hi']
"
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
add a comment |
You are creating a local dictionary in your add
function. Did you see that?
def add(a,b,c,d,container)
container = # it's a local new dict
# ...
Instead you should create the dictionary outside of the function, otherwise you'll always get a new dictionary containing only one key. For example:
container =
def add(a, b, c, d):
container[a] = b
container[c] = d
add a comment |
I recently made a module for this and posted it on github, it doesnt use recursion so therefore it has absolutely no limits to it. It allows you to edit, add, and remove from a nested dictionary using a keypath. Here it is on stackoverflow(this will answer your question):
How can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?
and here it is on github:
https://github.com/kthewhispers/Nested-Dictionary-Tools-Python/tree/master/src
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
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%2f55331045%2fi-want-to-create-function-that-keep-appending-new-data-to-nested-dictionary%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
i am creating a global_dict which is main dictionary where storing of all the
dict element is taken place.
for your given problem, if you want to add the data in a dict continously and that dict have same structure, then better:
is to create a default dict , and in that dict update the values and finally add that new small dict to the main dict using update method.
def funcion(a,b,c,d, container):
new_dic=a:['first':b,"Second":c,"third":d]
container.update(new_dic)
funcion('name', 1, 2, 'hi')
funcion('name1', 2, 1,'hi')
print(container)
"""
output
'name': ['first': 1, 'Second': 2, 'third': 'hi'],
'name1': ['first': 2, 'Second': 1, 'third': 'hi']
"
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
add a comment |
i am creating a global_dict which is main dictionary where storing of all the
dict element is taken place.
for your given problem, if you want to add the data in a dict continously and that dict have same structure, then better:
is to create a default dict , and in that dict update the values and finally add that new small dict to the main dict using update method.
def funcion(a,b,c,d, container):
new_dic=a:['first':b,"Second":c,"third":d]
container.update(new_dic)
funcion('name', 1, 2, 'hi')
funcion('name1', 2, 1,'hi')
print(container)
"""
output
'name': ['first': 1, 'Second': 2, 'third': 'hi'],
'name1': ['first': 2, 'Second': 1, 'third': 'hi']
"
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
add a comment |
i am creating a global_dict which is main dictionary where storing of all the
dict element is taken place.
for your given problem, if you want to add the data in a dict continously and that dict have same structure, then better:
is to create a default dict , and in that dict update the values and finally add that new small dict to the main dict using update method.
def funcion(a,b,c,d, container):
new_dic=a:['first':b,"Second":c,"third":d]
container.update(new_dic)
funcion('name', 1, 2, 'hi')
funcion('name1', 2, 1,'hi')
print(container)
"""
output
'name': ['first': 1, 'Second': 2, 'third': 'hi'],
'name1': ['first': 2, 'Second': 1, 'third': 'hi']
"
i am creating a global_dict which is main dictionary where storing of all the
dict element is taken place.
for your given problem, if you want to add the data in a dict continously and that dict have same structure, then better:
is to create a default dict , and in that dict update the values and finally add that new small dict to the main dict using update method.
def funcion(a,b,c,d, container):
new_dic=a:['first':b,"Second":c,"third":d]
container.update(new_dic)
funcion('name', 1, 2, 'hi')
funcion('name1', 2, 1,'hi')
print(container)
"""
output
'name': ['first': 1, 'Second': 2, 'third': 'hi'],
'name1': ['first': 2, 'Second': 1, 'third': 'hi']
"
edited Mar 25 at 4:09
answered Mar 25 at 3:59
prashant ranaprashant rana
2,20611123
2,20611123
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
add a comment |
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
Add some explanation is more informative.
– knh190
Mar 25 at 3:59
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
@knh190 done added explanation
– prashant rana
Mar 25 at 4:03
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
thank you for your comment. Do you have another way expect global method?
– Neal Lee
Mar 25 at 4:04
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee pass the main dictionary in the function and return the global_dict dictionary
– prashant rana
Mar 25 at 4:08
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
@NealLee updated the solution, check
– prashant rana
Mar 25 at 4:09
add a comment |
You are creating a local dictionary in your add
function. Did you see that?
def add(a,b,c,d,container)
container = # it's a local new dict
# ...
Instead you should create the dictionary outside of the function, otherwise you'll always get a new dictionary containing only one key. For example:
container =
def add(a, b, c, d):
container[a] = b
container[c] = d
add a comment |
You are creating a local dictionary in your add
function. Did you see that?
def add(a,b,c,d,container)
container = # it's a local new dict
# ...
Instead you should create the dictionary outside of the function, otherwise you'll always get a new dictionary containing only one key. For example:
container =
def add(a, b, c, d):
container[a] = b
container[c] = d
add a comment |
You are creating a local dictionary in your add
function. Did you see that?
def add(a,b,c,d,container)
container = # it's a local new dict
# ...
Instead you should create the dictionary outside of the function, otherwise you'll always get a new dictionary containing only one key. For example:
container =
def add(a, b, c, d):
container[a] = b
container[c] = d
You are creating a local dictionary in your add
function. Did you see that?
def add(a,b,c,d,container)
container = # it's a local new dict
# ...
Instead you should create the dictionary outside of the function, otherwise you'll always get a new dictionary containing only one key. For example:
container =
def add(a, b, c, d):
container[a] = b
container[c] = d
answered Mar 25 at 4:04
knh190knh190
1,708822
1,708822
add a comment |
add a comment |
I recently made a module for this and posted it on github, it doesnt use recursion so therefore it has absolutely no limits to it. It allows you to edit, add, and remove from a nested dictionary using a keypath. Here it is on stackoverflow(this will answer your question):
How can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?
and here it is on github:
https://github.com/kthewhispers/Nested-Dictionary-Tools-Python/tree/master/src
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
add a comment |
I recently made a module for this and posted it on github, it doesnt use recursion so therefore it has absolutely no limits to it. It allows you to edit, add, and remove from a nested dictionary using a keypath. Here it is on stackoverflow(this will answer your question):
How can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?
and here it is on github:
https://github.com/kthewhispers/Nested-Dictionary-Tools-Python/tree/master/src
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
add a comment |
I recently made a module for this and posted it on github, it doesnt use recursion so therefore it has absolutely no limits to it. It allows you to edit, add, and remove from a nested dictionary using a keypath. Here it is on stackoverflow(this will answer your question):
How can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?
and here it is on github:
https://github.com/kthewhispers/Nested-Dictionary-Tools-Python/tree/master/src
I recently made a module for this and posted it on github, it doesnt use recursion so therefore it has absolutely no limits to it. It allows you to edit, add, and remove from a nested dictionary using a keypath. Here it is on stackoverflow(this will answer your question):
How can you add entries, and retrieve, alter, or remove values from specific keys in any nested dictionary without recursion?
and here it is on github:
https://github.com/kthewhispers/Nested-Dictionary-Tools-Python/tree/master/src
answered Mar 25 at 4:07
Keith CroninKeith Cronin
1368
1368
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
add a comment |
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
Better to add solution here, your link might get delete in future, which will create problem for the future readers
– prashant rana
Mar 25 at 4:10
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%2f55331045%2fi-want-to-create-function-that-keep-appending-new-data-to-nested-dictionary%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
What's your
function
? Did you meanadd
?– knh190
Mar 25 at 4:01