Pass only non None values as argument to a function












-3















I have a for loop that returns a value or None depending on field being selected.



For Loop:



for fieldname, value in form.data.items():
if value is not None:
print(value)


Given below is the output of the print statement of the for loop:



store_name
None
store_location
None


I am trying to pass the output of the above loop statement to a function as shown below:



output = store_details() <<-- arguments to this function to only include non None items obtained from the above for loop statement


In the above example only store_name and store_location should be passes as arguments. How could I pass only the non None items as arguments? Please note the list arguments could vary each time hence I am trying to build this function such that the store_details function only passes non None values each time.



Update:



Given below is the loop I am trying to execute. This works fine if all the fields are selected (There are 5 fields in the page from which the user can select).



l = 
for fieldname, value in form.data.items():
if value is not None:
l.append(value)
print(l)

p = l[:-1]

output = store_details(*p)


If any of the fields are not selected, it returns a 'None' which throws



psycopg2.ProgrammingError: column "none" does not exist in table









share|improve this question

























  • What does form.data.items() return?

    – Rarblack
    Nov 23 '18 at 10:18











  • @Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

    – hello kee
    Nov 23 '18 at 10:21
















-3















I have a for loop that returns a value or None depending on field being selected.



For Loop:



for fieldname, value in form.data.items():
if value is not None:
print(value)


Given below is the output of the print statement of the for loop:



store_name
None
store_location
None


I am trying to pass the output of the above loop statement to a function as shown below:



output = store_details() <<-- arguments to this function to only include non None items obtained from the above for loop statement


In the above example only store_name and store_location should be passes as arguments. How could I pass only the non None items as arguments? Please note the list arguments could vary each time hence I am trying to build this function such that the store_details function only passes non None values each time.



Update:



Given below is the loop I am trying to execute. This works fine if all the fields are selected (There are 5 fields in the page from which the user can select).



l = 
for fieldname, value in form.data.items():
if value is not None:
l.append(value)
print(l)

p = l[:-1]

output = store_details(*p)


If any of the fields are not selected, it returns a 'None' which throws



psycopg2.ProgrammingError: column "none" does not exist in table









share|improve this question

























  • What does form.data.items() return?

    – Rarblack
    Nov 23 '18 at 10:18











  • @Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

    – hello kee
    Nov 23 '18 at 10:21














-3












-3








-3








I have a for loop that returns a value or None depending on field being selected.



For Loop:



for fieldname, value in form.data.items():
if value is not None:
print(value)


Given below is the output of the print statement of the for loop:



store_name
None
store_location
None


I am trying to pass the output of the above loop statement to a function as shown below:



output = store_details() <<-- arguments to this function to only include non None items obtained from the above for loop statement


In the above example only store_name and store_location should be passes as arguments. How could I pass only the non None items as arguments? Please note the list arguments could vary each time hence I am trying to build this function such that the store_details function only passes non None values each time.



Update:



Given below is the loop I am trying to execute. This works fine if all the fields are selected (There are 5 fields in the page from which the user can select).



l = 
for fieldname, value in form.data.items():
if value is not None:
l.append(value)
print(l)

p = l[:-1]

output = store_details(*p)


If any of the fields are not selected, it returns a 'None' which throws



psycopg2.ProgrammingError: column "none" does not exist in table









share|improve this question
















I have a for loop that returns a value or None depending on field being selected.



For Loop:



for fieldname, value in form.data.items():
if value is not None:
print(value)


Given below is the output of the print statement of the for loop:



store_name
None
store_location
None


I am trying to pass the output of the above loop statement to a function as shown below:



output = store_details() <<-- arguments to this function to only include non None items obtained from the above for loop statement


In the above example only store_name and store_location should be passes as arguments. How could I pass only the non None items as arguments? Please note the list arguments could vary each time hence I am trying to build this function such that the store_details function only passes non None values each time.



Update:



Given below is the loop I am trying to execute. This works fine if all the fields are selected (There are 5 fields in the page from which the user can select).



l = 
for fieldname, value in form.data.items():
if value is not None:
l.append(value)
print(l)

p = l[:-1]

output = store_details(*p)


If any of the fields are not selected, it returns a 'None' which throws



psycopg2.ProgrammingError: column "none" does not exist in table






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:14







hello kee

















asked Nov 23 '18 at 8:01









hello keehello kee

477




477













  • What does form.data.items() return?

    – Rarblack
    Nov 23 '18 at 10:18











  • @Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

    – hello kee
    Nov 23 '18 at 10:21



















  • What does form.data.items() return?

    – Rarblack
    Nov 23 '18 at 10:18











  • @Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

    – hello kee
    Nov 23 '18 at 10:21

















What does form.data.items() return?

– Rarblack
Nov 23 '18 at 10:18





What does form.data.items() return?

– Rarblack
Nov 23 '18 at 10:18













@Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

– hello kee
Nov 23 '18 at 10:21





@Rarblack, given below is what gets printed if I select a few fields. dict_items([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])

– hello kee
Nov 23 '18 at 10:21












2 Answers
2






active

oldest

votes


















0














I would recommend you to use list-comprehension:



You can do either:



output = store_details([value for fieldname, value in form.data.items() if not value == None])


or (you can do form.data.values() to to retrieve only values of dictionary)



output = store_details([value for value in form.data.values() if not value == None])


And you can also store the value of list-comprehension and pass it:



l = [value for value in form.data.values() if not value == None]
output = store_details(l)


EDIT : The error is because None has been written as a string in value, not an object None therefore if clause not catching it.



>>> a= dict([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])
>>> del a['csrf_token']
>>> a
{'store_name': 'None', 'store_type': 'store_type', 'store_location': 'store_location'}
>>> [i for i in a.values() if not i == 'None']
['store_type', 'store_location']
>>>


If you prefer of using your own code make this change:



l = 
a = form.data
del a['csrf_token'] # token field is removed

for fieldname, value in a.items():
if value is not 'None':
l.append(value)
print(l)

output = store_details(*l)





share|improve this answer


























  • thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

    – hello kee
    Nov 23 '18 at 9:45











  • It would be nice if you could share your full code or where error occurs it is hard to say like this

    – Rarblack
    Nov 23 '18 at 10:08













  • I have included the code which I am trying to execute. Hope this helps. Thanks..

    – hello kee
    Nov 23 '18 at 10:14











  • thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

    – hello kee
    Nov 23 '18 at 12:05











  • Is this error again happening?

    – Rarblack
    Nov 23 '18 at 12:08



















0














You can do this in many ways actually. One is a dictionary comprehension:



fields = {field:value for field,value in form.data.items() if value != None and value != 'None' and field != 'csrf_token'}
output = store_details(**fields)


UPDATE
I've included the 'None' check and excluded the csrf_token. Please use and operator, not the or.






share|improve this answer


























  • thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

    – hello kee
    Nov 23 '18 at 8:27











  • also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

    – hello kee
    Nov 23 '18 at 8:28











  • I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

    – hello kee
    Nov 23 '18 at 10:24











  • I've updated the response with the correct code for the conditional. Check it ;)

    – Borisu
    Nov 23 '18 at 14:48











  • thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

    – hello kee
    Nov 23 '18 at 15:25











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%2f53442704%2fpass-only-non-none-values-as-argument-to-a-function%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









0














I would recommend you to use list-comprehension:



You can do either:



output = store_details([value for fieldname, value in form.data.items() if not value == None])


or (you can do form.data.values() to to retrieve only values of dictionary)



output = store_details([value for value in form.data.values() if not value == None])


And you can also store the value of list-comprehension and pass it:



l = [value for value in form.data.values() if not value == None]
output = store_details(l)


EDIT : The error is because None has been written as a string in value, not an object None therefore if clause not catching it.



>>> a= dict([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])
>>> del a['csrf_token']
>>> a
{'store_name': 'None', 'store_type': 'store_type', 'store_location': 'store_location'}
>>> [i for i in a.values() if not i == 'None']
['store_type', 'store_location']
>>>


If you prefer of using your own code make this change:



l = 
a = form.data
del a['csrf_token'] # token field is removed

for fieldname, value in a.items():
if value is not 'None':
l.append(value)
print(l)

output = store_details(*l)





share|improve this answer


























  • thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

    – hello kee
    Nov 23 '18 at 9:45











  • It would be nice if you could share your full code or where error occurs it is hard to say like this

    – Rarblack
    Nov 23 '18 at 10:08













  • I have included the code which I am trying to execute. Hope this helps. Thanks..

    – hello kee
    Nov 23 '18 at 10:14











  • thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

    – hello kee
    Nov 23 '18 at 12:05











  • Is this error again happening?

    – Rarblack
    Nov 23 '18 at 12:08
















0














I would recommend you to use list-comprehension:



You can do either:



output = store_details([value for fieldname, value in form.data.items() if not value == None])


or (you can do form.data.values() to to retrieve only values of dictionary)



output = store_details([value for value in form.data.values() if not value == None])


And you can also store the value of list-comprehension and pass it:



l = [value for value in form.data.values() if not value == None]
output = store_details(l)


EDIT : The error is because None has been written as a string in value, not an object None therefore if clause not catching it.



>>> a= dict([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])
>>> del a['csrf_token']
>>> a
{'store_name': 'None', 'store_type': 'store_type', 'store_location': 'store_location'}
>>> [i for i in a.values() if not i == 'None']
['store_type', 'store_location']
>>>


If you prefer of using your own code make this change:



l = 
a = form.data
del a['csrf_token'] # token field is removed

for fieldname, value in a.items():
if value is not 'None':
l.append(value)
print(l)

output = store_details(*l)





share|improve this answer


























  • thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

    – hello kee
    Nov 23 '18 at 9:45











  • It would be nice if you could share your full code or where error occurs it is hard to say like this

    – Rarblack
    Nov 23 '18 at 10:08













  • I have included the code which I am trying to execute. Hope this helps. Thanks..

    – hello kee
    Nov 23 '18 at 10:14











  • thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

    – hello kee
    Nov 23 '18 at 12:05











  • Is this error again happening?

    – Rarblack
    Nov 23 '18 at 12:08














0












0








0







I would recommend you to use list-comprehension:



You can do either:



output = store_details([value for fieldname, value in form.data.items() if not value == None])


or (you can do form.data.values() to to retrieve only values of dictionary)



output = store_details([value for value in form.data.values() if not value == None])


And you can also store the value of list-comprehension and pass it:



l = [value for value in form.data.values() if not value == None]
output = store_details(l)


EDIT : The error is because None has been written as a string in value, not an object None therefore if clause not catching it.



>>> a= dict([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])
>>> del a['csrf_token']
>>> a
{'store_name': 'None', 'store_type': 'store_type', 'store_location': 'store_location'}
>>> [i for i in a.values() if not i == 'None']
['store_type', 'store_location']
>>>


If you prefer of using your own code make this change:



l = 
a = form.data
del a['csrf_token'] # token field is removed

for fieldname, value in a.items():
if value is not 'None':
l.append(value)
print(l)

output = store_details(*l)





share|improve this answer















I would recommend you to use list-comprehension:



You can do either:



output = store_details([value for fieldname, value in form.data.items() if not value == None])


or (you can do form.data.values() to to retrieve only values of dictionary)



output = store_details([value for value in form.data.values() if not value == None])


And you can also store the value of list-comprehension and pass it:



l = [value for value in form.data.values() if not value == None]
output = store_details(l)


EDIT : The error is because None has been written as a string in value, not an object None therefore if clause not catching it.



>>> a= dict([('store_name', 'None'), ('store_type', 'store_type'), ('store_location', 'store_location'), ('csrf_token', 'ImI0MzQyYzcwZDk5NTVmMGQxMWY5YTk3NDhmNTNkNzJmMjQ5NWIzZTIi.Dtllqg.-SXYDYkBmQiPjSmo01VnDgSbZrE')])
>>> del a['csrf_token']
>>> a
{'store_name': 'None', 'store_type': 'store_type', 'store_location': 'store_location'}
>>> [i for i in a.values() if not i == 'None']
['store_type', 'store_location']
>>>


If you prefer of using your own code make this change:



l = 
a = form.data
del a['csrf_token'] # token field is removed

for fieldname, value in a.items():
if value is not 'None':
l.append(value)
print(l)

output = store_details(*l)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:37

























answered Nov 23 '18 at 9:28









RarblackRarblack

2,78541025




2,78541025













  • thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

    – hello kee
    Nov 23 '18 at 9:45











  • It would be nice if you could share your full code or where error occurs it is hard to say like this

    – Rarblack
    Nov 23 '18 at 10:08













  • I have included the code which I am trying to execute. Hope this helps. Thanks..

    – hello kee
    Nov 23 '18 at 10:14











  • thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

    – hello kee
    Nov 23 '18 at 12:05











  • Is this error again happening?

    – Rarblack
    Nov 23 '18 at 12:08



















  • thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

    – hello kee
    Nov 23 '18 at 9:45











  • It would be nice if you could share your full code or where error occurs it is hard to say like this

    – Rarblack
    Nov 23 '18 at 10:08













  • I have included the code which I am trying to execute. Hope this helps. Thanks..

    – hello kee
    Nov 23 '18 at 10:14











  • thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

    – hello kee
    Nov 23 '18 at 12:05











  • Is this error again happening?

    – Rarblack
    Nov 23 '18 at 12:08

















thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

– hello kee
Nov 23 '18 at 9:45





thanks for your reply.. I get the below error: psycopg2.ProgrammingError: syntax error at or near "]" LINE 1: select from table_name

– hello kee
Nov 23 '18 at 9:45













It would be nice if you could share your full code or where error occurs it is hard to say like this

– Rarblack
Nov 23 '18 at 10:08







It would be nice if you could share your full code or where error occurs it is hard to say like this

– Rarblack
Nov 23 '18 at 10:08















I have included the code which I am trying to execute. Hope this helps. Thanks..

– hello kee
Nov 23 '18 at 10:14





I have included the code which I am trying to execute. Hope this helps. Thanks..

– hello kee
Nov 23 '18 at 10:14













thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

– hello kee
Nov 23 '18 at 12:05





thanks for the reply. I tried with your modified code and find that if all fields are selected it works fine.. However if one of the fields are not selected, it throws an error "psycopg2.ProgrammingError: column "none" does not exist in table".. Could you please assist on this.. Thnx.

– hello kee
Nov 23 '18 at 12:05













Is this error again happening?

– Rarblack
Nov 23 '18 at 12:08





Is this error again happening?

– Rarblack
Nov 23 '18 at 12:08













0














You can do this in many ways actually. One is a dictionary comprehension:



fields = {field:value for field,value in form.data.items() if value != None and value != 'None' and field != 'csrf_token'}
output = store_details(**fields)


UPDATE
I've included the 'None' check and excluded the csrf_token. Please use and operator, not the or.






share|improve this answer


























  • thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

    – hello kee
    Nov 23 '18 at 8:27











  • also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

    – hello kee
    Nov 23 '18 at 8:28











  • I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

    – hello kee
    Nov 23 '18 at 10:24











  • I've updated the response with the correct code for the conditional. Check it ;)

    – Borisu
    Nov 23 '18 at 14:48











  • thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

    – hello kee
    Nov 23 '18 at 15:25
















0














You can do this in many ways actually. One is a dictionary comprehension:



fields = {field:value for field,value in form.data.items() if value != None and value != 'None' and field != 'csrf_token'}
output = store_details(**fields)


UPDATE
I've included the 'None' check and excluded the csrf_token. Please use and operator, not the or.






share|improve this answer


























  • thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

    – hello kee
    Nov 23 '18 at 8:27











  • also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

    – hello kee
    Nov 23 '18 at 8:28











  • I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

    – hello kee
    Nov 23 '18 at 10:24











  • I've updated the response with the correct code for the conditional. Check it ;)

    – Borisu
    Nov 23 '18 at 14:48











  • thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

    – hello kee
    Nov 23 '18 at 15:25














0












0








0







You can do this in many ways actually. One is a dictionary comprehension:



fields = {field:value for field,value in form.data.items() if value != None and value != 'None' and field != 'csrf_token'}
output = store_details(**fields)


UPDATE
I've included the 'None' check and excluded the csrf_token. Please use and operator, not the or.






share|improve this answer















You can do this in many ways actually. One is a dictionary comprehension:



fields = {field:value for field,value in form.data.items() if value != None and value != 'None' and field != 'csrf_token'}
output = store_details(**fields)


UPDATE
I've included the 'None' check and excluded the csrf_token. Please use and operator, not the or.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 12:14

























answered Nov 23 '18 at 8:14









BorisuBorisu

42528




42528













  • thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

    – hello kee
    Nov 23 '18 at 8:27











  • also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

    – hello kee
    Nov 23 '18 at 8:28











  • I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

    – hello kee
    Nov 23 '18 at 10:24











  • I've updated the response with the correct code for the conditional. Check it ;)

    – Borisu
    Nov 23 '18 at 14:48











  • thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

    – hello kee
    Nov 23 '18 at 15:25



















  • thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

    – hello kee
    Nov 23 '18 at 8:27











  • also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

    – hello kee
    Nov 23 '18 at 8:28











  • I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

    – hello kee
    Nov 23 '18 at 10:24











  • I've updated the response with the correct code for the conditional. Check it ;)

    – Borisu
    Nov 23 '18 at 14:48











  • thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

    – hello kee
    Nov 23 '18 at 15:25

















thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

– hello kee
Nov 23 '18 at 8:27





thanks for the reply. I encountered an issue where in both the key and value pair are returned as below. {'store_name' : 'store_name' , 'store_id' : 'None', 'store_location' : 'store_location' , 'store_address' : 'None', 'csrf_token' : 'feferf'} how could I only return the value alone and exclude all None

– hello kee
Nov 23 '18 at 8:27













also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

– hello kee
Nov 23 '18 at 8:28





also could you please advice how I could also not pass in the last key value - pair ('csrf_token' : 'feferf')

– hello kee
Nov 23 '18 at 8:28













I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

– hello kee
Nov 23 '18 at 10:24





I have modified as per your latest code and I get the error psycopg2.ProgrammingError: column "csrf_token" does not exist in table...

– hello kee
Nov 23 '18 at 10:24













I've updated the response with the correct code for the conditional. Check it ;)

– Borisu
Nov 23 '18 at 14:48





I've updated the response with the correct code for the conditional. Check it ;)

– Borisu
Nov 23 '18 at 14:48













thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

– hello kee
Nov 23 '18 at 15:25





thanks for the update, tried with the modified code.. It throws an error saying -- "TypeError: store_details() got an unexpected keyword argument 'store_id'" .. store_id is the first column my list

– hello kee
Nov 23 '18 at 15:25


















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%2f53442704%2fpass-only-non-none-values-as-argument-to-a-function%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'