How should we refer a value of a key from a dictionary in one function (i.e. method) into another function ? My piece of python code is as below -
def function1 (x1, x2):
dict1 = {'k1':x1, 'k2':x2}
return
def function2 ():
# Here function1() is being called and passing 10 into x1 and 20 into x2 of function1 ()...
function1(10,20)
print(f"First Value is: {dict1[k1]}"
f"Second Value is: {dict1[k2]}")
I want the result as below -
First value is: 10
Second value is: 20
But I'm not able to ACCESS "{dict1[k1]}" and "{dict1[k2]}" key-value of dict1{} which is from function1() inside the function2(). So, how to access the Keys-values of a dictionary from one function inside another function?
There are several ways to achieve this, one could be to return the whole dictionary and loop through this new dictionary, however I would implement it as so:
def function1 (x1, x2):
dict1 = {'k1':x1, 'k2':x2}
return dict1.values()
def function2 ():
# Here function1() is being called and passing 10 into x1 and 20 into x2 of function1 ()...
x, y = function1(10,20)
print(f"First Value is: {x}"
f"\nSecond Value is: {y}")
function2()
By returning only the values you can avoid loops
Output:
First Value is: 10
Second Value is: 20