Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop through **kwargs in Python?

In the code below, I want to read obj.subject and place it into var subject, also read obj.body and place it into body. First I want to read the kwargs variables and search for keywords within the string to replace, if none exists then move on.

How can I iterate through kwargs in Python?

for key in kwargs:
    subject = str(obj.subject).replace('[%s]' % upper(key), kwargs[key])

for key in kwargs:
    body = str(obj.body).replace('[%s]' % upper(key), kwargs[key])

return (subject, body, obj.is_html)
like image 617
Mo J. Mughrabi Avatar asked Jan 17 '12 17:01

Mo J. Mughrabi


People also ask

How do you pass in Kwargs in Python?

Use the Python **kwargs parameter to allow the function to accept a variable number of keyword arguments. Inside the function, the kwargs argument is a dictionary that contains all keyword arguments as its name-value pairs. Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter.

How do you pass Kwargs between functions?

Kwargs allow you to pass keyword arguments to a function. They are used when you are not sure of the number of keyword arguments that will be passed in the function. Kwargs can be used for unpacking dictionary key, value pairs. This is done using the double asterisk notation ( ** ).

Can you have multiple Kwargs in Python?

Python 3.5+ allows passing multiple sets of keyword arguments ("kwargs") to a function within a single call, using the `"**"` syntax.


3 Answers

For Python 3 users:

You can iterate through kwargs with .items()

subject = obj.subject
body = obj.body
for key, value in kwargs.items():
    subject = subject.replace('[%s]' % key.toupper(), value)
    body = body.replace('[%s]' % key.toupper(), value)

return (subject, body, obj.is_html)

For Python 2 users:

You can iterate through kwargs with .iteritems():

subject = obj.subject
body = obj.body
for key, value in kwargs.iteritems():
    subject = subject.replace('[%s]' % key.toupper(), value)
    body = body.replace('[%s]' % key.toupper(), value)

return (subject, body, obj.is_html)
like image 82
jterrace Avatar answered Oct 14 '22 09:10

jterrace


Just a quick note for those upgrading to Python 3.

In Python 3 it's almost the same:

subject = obj.subject
body = obj.body
for key, value in kwargs.items():
    subject = subject.replace('[{0}]'.format(key.toupper()), value)
    body = body.replace('[{0}]'.format(key.toupper()), value)

return (subject, body, obj.is_html)

Notice that iteritems() becomes items() as dict no longer has the method iteritems.

like image 30
oliverseal Avatar answered Oct 14 '22 09:10

oliverseal


you can iter on the dictionary as it provides by default iteration on its key.

subject = obj.subject
body = obj.body
    
for key in kwargs:
    subject = str(subject).replace([%s] % upper(key), kwargs[key])
    body = str(body).replace([%s] % upper(key), kwargs[key])

print(subject, body)

I tested it on Python 3 and Python 2 Interpreters and it is working totally fine for me so it will also work fine for you. There is a default for all the structures whether its List or Dictionary or tuple you can Iter on it easily in the python language.

Attaching a photo depicting that we can do that so easily. Photo For Proof

like image 45
3 revs Avatar answered Oct 14 '22 10:10

3 revs