Python: Passing Dictionary as Arguments to Function

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

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

# Python program to demonstrate
# passing dictionary as argument
# A function that takes dictionary
# as an argument
def func(d):
for key in d:
print(“key:”, key, “Value:”, d[key])
# Driver’s code
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.

# Python program to demonstrate
# passing dictionary as kwargs
def display(**name):
print (name[“fname”]+” “+name[“mname”]+” “+name[“lname”])
def main():
# passing dictionary key-value
# pair as arguments
display(fname =”John”,
mname =”F.”,
lname =”Kennedy”)
# Driver’s code
main()

Tip solutie

Permanent

Voteaza

(5 din 11 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?