how to replace a function variable with a value from a dictionary in Python











up vote
-2
down vote

favorite












I am trying to loop through the below dictionary and replace the "string" variable inside the function with the values from the dictionary and then execute the function. I am thinking about writing a for loop and then include the function inside the for loop. Please help.



expected function:




def my_function(fname):
string = value  
print(string + " Refsnes")

my_function("string")



 dict = {"kafka":[{
"value":"I am"},
{"value":"You are"},
{"value":"They are"}
]}

def my_function(fname):
string = "I am"  
print(string + " Refsnes")

my_function("string")









share|improve this question
























  • The function does nothing with the value passed as an argument. It's not clear what you want from this.
    – roganjosh
    Nov 21 at 18:35










  • I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
    – Mona
    Nov 21 at 18:41















up vote
-2
down vote

favorite












I am trying to loop through the below dictionary and replace the "string" variable inside the function with the values from the dictionary and then execute the function. I am thinking about writing a for loop and then include the function inside the for loop. Please help.



expected function:




def my_function(fname):
string = value  
print(string + " Refsnes")

my_function("string")



 dict = {"kafka":[{
"value":"I am"},
{"value":"You are"},
{"value":"They are"}
]}

def my_function(fname):
string = "I am"  
print(string + " Refsnes")

my_function("string")









share|improve this question
























  • The function does nothing with the value passed as an argument. It's not clear what you want from this.
    – roganjosh
    Nov 21 at 18:35










  • I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
    – Mona
    Nov 21 at 18:41













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am trying to loop through the below dictionary and replace the "string" variable inside the function with the values from the dictionary and then execute the function. I am thinking about writing a for loop and then include the function inside the for loop. Please help.



expected function:




def my_function(fname):
string = value  
print(string + " Refsnes")

my_function("string")



 dict = {"kafka":[{
"value":"I am"},
{"value":"You are"},
{"value":"They are"}
]}

def my_function(fname):
string = "I am"  
print(string + " Refsnes")

my_function("string")









share|improve this question















I am trying to loop through the below dictionary and replace the "string" variable inside the function with the values from the dictionary and then execute the function. I am thinking about writing a for loop and then include the function inside the for loop. Please help.



expected function:




def my_function(fname):
string = value  
print(string + " Refsnes")

my_function("string")



 dict = {"kafka":[{
"value":"I am"},
{"value":"You are"},
{"value":"They are"}
]}

def my_function(fname):
string = "I am"  
print(string + " Refsnes")

my_function("string")






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:41









eyllanesc

70.8k93052




70.8k93052










asked Nov 21 at 18:33









Mona

95




95












  • The function does nothing with the value passed as an argument. It's not clear what you want from this.
    – roganjosh
    Nov 21 at 18:35










  • I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
    – Mona
    Nov 21 at 18:41


















  • The function does nothing with the value passed as an argument. It's not clear what you want from this.
    – roganjosh
    Nov 21 at 18:35










  • I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
    – Mona
    Nov 21 at 18:41
















The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 at 18:35




The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 at 18:35












I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
– Mona
Nov 21 at 18:41




I am basically trying to replace the "string" variable inside the function with the values from dictionary and run the function. I want to loop through the dictionary values and run the function. @roganjosh
– Mona
Nov 21 at 18:41












2 Answers
2






active

oldest

votes

















up vote
0
down vote













The best thing would be to first modify your dict. Right now "kafka" maps to a list of dicts that all have the same key, so why bother and have a key in the first place.
So you could have a dict like:



dictionary = {"kafka": ["I am", "You are", "They are"]}


Also do not call the variable dict, because you would overwrite the built-in function dict, which makes it unusable.



Then you also have to change your function to actually make use of the paramter.



def my_function(string):
print(string + " Refsnes")


And now, to loop over the dict an print the function with all the values you have two options.



First loop over the list in the dictionary and call the function repeatedly:



for string in dictionary["kafka"]:
my_function(string)


Or give a list to your function and loop over the list in the function. Your function would then look like this:



def my_function(list_of_strings):
for string in list_of_strings:
print(string + " Refsnes")

# Call function with
my_function(dictionary["kafka"])





share|improve this answer




























    up vote
    0
    down vote













    A couple of things here. Did you get the dictionary from elsewhere, or are you setting it up yourself? And is there a specific reason you need to define a function? If you're setting it up yourself, I would advise you to use a list instead of a dictionary, and to just do a for loop all by itself, like this:



    kafka = ['I am ', 'You are ', 'They are']
    for k in kafka:
    print(k + 'Refsnes')


    If you need it to be a dictionary and you need it to be a function, I would approach it like this. (Note that you don't need a function to pull an item out of a dictionary, so I would actually put the loop inside the function.)



    kafkadict = {"first person": "I am", "second person": "You are", "third person plural": "They are"}
    def printKafkaStuff(dictinput):
    for k in dictinput.values():
    print(k + " Refsnes")


    Then call it with



    printKafkaStuff(kafkadict)





    share|improve this answer























    • Still works for me - maybe a difference across different versions of Python?
      – Ellie Hanna
      Nov 21 at 19:03










    • gotcha - thanks, I'll edit my answer
      – Ellie Hanna
      Nov 21 at 19:06











    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%2f53418497%2fhow-to-replace-a-function-variable-with-a-value-from-a-dictionary-in-python%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








    up vote
    0
    down vote













    The best thing would be to first modify your dict. Right now "kafka" maps to a list of dicts that all have the same key, so why bother and have a key in the first place.
    So you could have a dict like:



    dictionary = {"kafka": ["I am", "You are", "They are"]}


    Also do not call the variable dict, because you would overwrite the built-in function dict, which makes it unusable.



    Then you also have to change your function to actually make use of the paramter.



    def my_function(string):
    print(string + " Refsnes")


    And now, to loop over the dict an print the function with all the values you have two options.



    First loop over the list in the dictionary and call the function repeatedly:



    for string in dictionary["kafka"]:
    my_function(string)


    Or give a list to your function and loop over the list in the function. Your function would then look like this:



    def my_function(list_of_strings):
    for string in list_of_strings:
    print(string + " Refsnes")

    # Call function with
    my_function(dictionary["kafka"])





    share|improve this answer

























      up vote
      0
      down vote













      The best thing would be to first modify your dict. Right now "kafka" maps to a list of dicts that all have the same key, so why bother and have a key in the first place.
      So you could have a dict like:



      dictionary = {"kafka": ["I am", "You are", "They are"]}


      Also do not call the variable dict, because you would overwrite the built-in function dict, which makes it unusable.



      Then you also have to change your function to actually make use of the paramter.



      def my_function(string):
      print(string + " Refsnes")


      And now, to loop over the dict an print the function with all the values you have two options.



      First loop over the list in the dictionary and call the function repeatedly:



      for string in dictionary["kafka"]:
      my_function(string)


      Or give a list to your function and loop over the list in the function. Your function would then look like this:



      def my_function(list_of_strings):
      for string in list_of_strings:
      print(string + " Refsnes")

      # Call function with
      my_function(dictionary["kafka"])





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The best thing would be to first modify your dict. Right now "kafka" maps to a list of dicts that all have the same key, so why bother and have a key in the first place.
        So you could have a dict like:



        dictionary = {"kafka": ["I am", "You are", "They are"]}


        Also do not call the variable dict, because you would overwrite the built-in function dict, which makes it unusable.



        Then you also have to change your function to actually make use of the paramter.



        def my_function(string):
        print(string + " Refsnes")


        And now, to loop over the dict an print the function with all the values you have two options.



        First loop over the list in the dictionary and call the function repeatedly:



        for string in dictionary["kafka"]:
        my_function(string)


        Or give a list to your function and loop over the list in the function. Your function would then look like this:



        def my_function(list_of_strings):
        for string in list_of_strings:
        print(string + " Refsnes")

        # Call function with
        my_function(dictionary["kafka"])





        share|improve this answer












        The best thing would be to first modify your dict. Right now "kafka" maps to a list of dicts that all have the same key, so why bother and have a key in the first place.
        So you could have a dict like:



        dictionary = {"kafka": ["I am", "You are", "They are"]}


        Also do not call the variable dict, because you would overwrite the built-in function dict, which makes it unusable.



        Then you also have to change your function to actually make use of the paramter.



        def my_function(string):
        print(string + " Refsnes")


        And now, to loop over the dict an print the function with all the values you have two options.



        First loop over the list in the dictionary and call the function repeatedly:



        for string in dictionary["kafka"]:
        my_function(string)


        Or give a list to your function and loop over the list in the function. Your function would then look like this:



        def my_function(list_of_strings):
        for string in list_of_strings:
        print(string + " Refsnes")

        # Call function with
        my_function(dictionary["kafka"])






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 18:57









        FChris

        948




        948
























            up vote
            0
            down vote













            A couple of things here. Did you get the dictionary from elsewhere, or are you setting it up yourself? And is there a specific reason you need to define a function? If you're setting it up yourself, I would advise you to use a list instead of a dictionary, and to just do a for loop all by itself, like this:



            kafka = ['I am ', 'You are ', 'They are']
            for k in kafka:
            print(k + 'Refsnes')


            If you need it to be a dictionary and you need it to be a function, I would approach it like this. (Note that you don't need a function to pull an item out of a dictionary, so I would actually put the loop inside the function.)



            kafkadict = {"first person": "I am", "second person": "You are", "third person plural": "They are"}
            def printKafkaStuff(dictinput):
            for k in dictinput.values():
            print(k + " Refsnes")


            Then call it with



            printKafkaStuff(kafkadict)





            share|improve this answer























            • Still works for me - maybe a difference across different versions of Python?
              – Ellie Hanna
              Nov 21 at 19:03










            • gotcha - thanks, I'll edit my answer
              – Ellie Hanna
              Nov 21 at 19:06















            up vote
            0
            down vote













            A couple of things here. Did you get the dictionary from elsewhere, or are you setting it up yourself? And is there a specific reason you need to define a function? If you're setting it up yourself, I would advise you to use a list instead of a dictionary, and to just do a for loop all by itself, like this:



            kafka = ['I am ', 'You are ', 'They are']
            for k in kafka:
            print(k + 'Refsnes')


            If you need it to be a dictionary and you need it to be a function, I would approach it like this. (Note that you don't need a function to pull an item out of a dictionary, so I would actually put the loop inside the function.)



            kafkadict = {"first person": "I am", "second person": "You are", "third person plural": "They are"}
            def printKafkaStuff(dictinput):
            for k in dictinput.values():
            print(k + " Refsnes")


            Then call it with



            printKafkaStuff(kafkadict)





            share|improve this answer























            • Still works for me - maybe a difference across different versions of Python?
              – Ellie Hanna
              Nov 21 at 19:03










            • gotcha - thanks, I'll edit my answer
              – Ellie Hanna
              Nov 21 at 19:06













            up vote
            0
            down vote










            up vote
            0
            down vote









            A couple of things here. Did you get the dictionary from elsewhere, or are you setting it up yourself? And is there a specific reason you need to define a function? If you're setting it up yourself, I would advise you to use a list instead of a dictionary, and to just do a for loop all by itself, like this:



            kafka = ['I am ', 'You are ', 'They are']
            for k in kafka:
            print(k + 'Refsnes')


            If you need it to be a dictionary and you need it to be a function, I would approach it like this. (Note that you don't need a function to pull an item out of a dictionary, so I would actually put the loop inside the function.)



            kafkadict = {"first person": "I am", "second person": "You are", "third person plural": "They are"}
            def printKafkaStuff(dictinput):
            for k in dictinput.values():
            print(k + " Refsnes")


            Then call it with



            printKafkaStuff(kafkadict)





            share|improve this answer














            A couple of things here. Did you get the dictionary from elsewhere, or are you setting it up yourself? And is there a specific reason you need to define a function? If you're setting it up yourself, I would advise you to use a list instead of a dictionary, and to just do a for loop all by itself, like this:



            kafka = ['I am ', 'You are ', 'They are']
            for k in kafka:
            print(k + 'Refsnes')


            If you need it to be a dictionary and you need it to be a function, I would approach it like this. (Note that you don't need a function to pull an item out of a dictionary, so I would actually put the loop inside the function.)



            kafkadict = {"first person": "I am", "second person": "You are", "third person plural": "They are"}
            def printKafkaStuff(dictinput):
            for k in dictinput.values():
            print(k + " Refsnes")


            Then call it with



            printKafkaStuff(kafkadict)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 19:06

























            answered Nov 21 at 18:50









            Ellie Hanna

            484




            484












            • Still works for me - maybe a difference across different versions of Python?
              – Ellie Hanna
              Nov 21 at 19:03










            • gotcha - thanks, I'll edit my answer
              – Ellie Hanna
              Nov 21 at 19:06


















            • Still works for me - maybe a difference across different versions of Python?
              – Ellie Hanna
              Nov 21 at 19:03










            • gotcha - thanks, I'll edit my answer
              – Ellie Hanna
              Nov 21 at 19:06
















            Still works for me - maybe a difference across different versions of Python?
            – Ellie Hanna
            Nov 21 at 19:03




            Still works for me - maybe a difference across different versions of Python?
            – Ellie Hanna
            Nov 21 at 19:03












            gotcha - thanks, I'll edit my answer
            – Ellie Hanna
            Nov 21 at 19:06




            gotcha - thanks, I'll edit my answer
            – Ellie Hanna
            Nov 21 at 19:06


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53418497%2fhow-to-replace-a-function-variable-with-a-value-from-a-dictionary-in-python%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

            Berounka

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Sphinx de Gizeh