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

Submitted 5 years, 1 month ago
Ticket #154
Views 395
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 5 years, 1 month 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 5 years ago

thank you @Nmadhusmita018

- Soudhamini 5 years ago


Latest Blogs