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;








0















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!










share|improve this question
























  • What's your function? Did you mean add?

    – knh190
    Mar 25 at 4:01

















0















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!










share|improve this question
























  • What's your function? Did you mean add?

    – knh190
    Mar 25 at 4:01













0












0








0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 4:18







Neal Lee

















asked Mar 25 at 3:51









Neal LeeNeal Lee

32




32












  • 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
















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












3 Answers
3






active

oldest

votes


















0














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']

"





share|improve this answer

























  • 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


















1














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





share|improve this answer






























    -1














    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






    share|improve this answer























    • 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











    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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']

    "





    share|improve this answer

























    • 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















    0














    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']

    "





    share|improve this answer

























    • 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













    0












    0








    0







    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']

    "





    share|improve this answer















    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']

    "






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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













    1














    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





    share|improve this answer



























      1














      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





      share|improve this answer

























        1












        1








        1







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 4:04









        knh190knh190

        1,708822




        1,708822





















            -1














            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






            share|improve this answer























            • 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















            -1














            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






            share|improve this answer























            • 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













            -1












            -1








            -1







            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






            share|improve this answer













            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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

















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현