Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How can we return a dictionary from a Python function?

I have a dictionary given below

{'x': 50, 'str': 'Tutorialspoint'}

 How do I return this dictionary or any given dictionary from a python function?


1 Answer
Manogna

There can be any number of ways we can return a dictionary from a python function. Consider the one given below.

# This function returns a dictionary
def foo():
     d = dict();
     d['str'] = "Tutorialspoint"
     d['x']   = 50
     return d
print foo()

OUTPUT

{'x': 50, 'str': 'Tutorialspoint'}
 
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.