Passing the keys of dictionary as parameters in function and returning their arguments as output

Submitted 4 years, 9 months ago
Ticket #154
Views 329
Language/Framework Python
Priority Medium
Status Closed

I want a function like

​
d = dict(p1=1, p2=2)
def f2(p1, p2):
    print(p1, p2)
f2(d)

​

which returns output

1 2

IS THIS POSSIBLE????

Submitted on Sep 10, 20
add a comment

2 Answers

d = dict(p1=1, p2=7,p3=3)
def f2(p1, p2,p3):
    print(p1,p2,p3)
f2(**d)

output:

1 7 3

Submitted 4 years, 9 months ago

In Python, everything is an object, so the dictionary can be passed as an argument to a function like other variables are passed.

Submitted 4 years, 8 months ago

thank you @Nmadhusmita018

- Soudhamini 4 years, 8 months ago


Latest Blogs