A dictionary in Python is a collection of data which is unordered and mutable. Unlike, numeric indices used by lists, a dictionary uses the key as an index for a specific value. It can be used to store unrelated data types but data that is related as a real-world entity. The keys themselves are employed for using a specific value.
Refer to the below article to get the idea about Python Dictionary.
Passing Dictionary as an argument
In Python, everything is an object, so the dictionary can be passed as an argument to a function like other variables are passed.
Example:
def func(d):
for key in d:
print("key:", key, "Value:", d[key])
D = {'a':1, 'b':2, 'c':3}
func(D)
|
Output:
key: b Value: 2
key: a Value: 1
key: c Value: 3
Passing Dictionary as kwargs
“kwargs” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn’t have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.
Example 1:
def display(**name):
print (name["fname"]+" "+name["mname"]+" "+name["lname"])
def main():
display(fname ="John",
mname ="F.",
lname ="Kennedy")
main()
|
Output:
John F. Kennedy
Example 2:
def display(x = 0, y = 0, **name):
print (name["fname"]+" "+name["mname"]+" "+name["lname"])
print ("x =", x)
print ("y =", y)
def main():
display(2, fname ="John", mname ="F.", lname ="Kennedy")
main()
|
Output:
John F. Kennedy
x = 2
y = 0
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
30 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...