how to replace a function variable with a value from a dictionary in Python
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
add a comment |
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
The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 '18 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 '18 at 18:41
add a comment |
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
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
python
edited Nov 21 '18 at 18:41
eyllanesc
74.6k103156
74.6k103156
asked Nov 21 '18 at 18:33
MonaMona
215
215
The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 '18 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 '18 at 18:41
add a comment |
The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 18:41
add a comment |
2 Answers
2
active
oldest
votes
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"])
add a comment |
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)
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
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%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
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"])
add a comment |
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"])
add a comment |
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"])
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"])
answered Nov 21 '18 at 18:57
FChrisFChris
13310
13310
add a comment |
add a comment |
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)
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
add a comment |
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)
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
add a comment |
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)
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)
edited Nov 21 '18 at 19:06
answered Nov 21 '18 at 18:50
Ellie HannaEllie Hanna
484
484
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
add a comment |
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
Still works for me - maybe a difference across different versions of Python?
– Ellie Hanna
Nov 21 '18 at 19:03
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
gotcha - thanks, I'll edit my answer
– Ellie Hanna
Nov 21 '18 at 19:06
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.
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.
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%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
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
The function does nothing with the value passed as an argument. It's not clear what you want from this.
– roganjosh
Nov 21 '18 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 '18 at 18:41