def my_function():
val_1 = "devsheet"
val_2 = "hello world"
return [val_1, val_2];
items_list = my_function()
print(items_list[0])
#-> "devsheet"
print(items_list[1])
#-> "hello world"
You can also use python list to return multiple values from a function. List are created using [] in python and you can add multiple values to it.In the above code snippet we are adding two values to the list and returning it from a function. After getting it, we are accessing them using list index.
def func():
vals = {}
vals['name'] = 'John'
vals['address'] = 'New York'
return vals
values = func()
print(values['name'])
# -> "John"
print(values['address'])
# -> "New York"
To return multiple values from a function, a python dictionary can also be used and you can add multiple key-value pairs to it and return them. You can access the values of a dictionary using its key names.
In the code snippets we are returning two values name and address using dict.
0 Comments