# How to call nested tuple and nested set or dictionary using variable argument and variable keyword argument methods ?

Anonymous

.

def arithmetic_mean(first, *values):
""" This function calculates the arithmetic mean of a non-empty
arbitrary number of numerical values """

return (first + sum(values)) / (1 + len(values))

x= [('a', 232), ('b', 343), ('c', 543), ('d', 23)]
y= [[('a', 232), ('b', 343), ('c', 543), ('d', 23)]]


how to pass x and y inside arithmetic_mean. can it be possible through zip method?

edit retag close merge delete

Sort by ยป oldest newest most voted

I am not sure about your exact question, does the following answer it ?

sage: arithmetic_mean(*dict(x).values())
1141/4
sage: arithmetic_mean(*dict(y[0]).values())
1141/4


EDIT Here is a version using zip (though it is not very natural):

sage: arithmetic_mean(*zip(*x)[1])
1141/4
sage: arithmetic_mean(*zip(*y[0])[1])
1141/4

more

thanq thanq very much it solved my problem

i got information from site " https://www.python-course.eu/python3_functions.php%22 (https://www.python-course.eu/python3_...) in 'Arbitrary Number of Parameters' section that, This type of problem can be solved by zip metyhod. can u tell me, is it possible or not.

( 2018-10-12 21:50:19 +0100 )edit

Everything is always possible (see my edit), but the main question is which problem do you want to solve.

( 2018-10-12 22:36:55 +0100 )edit

Thanq very much. i checked

print(arithmetic_mean(45,list(zip(x))[1])) ; print(arithmetic_mean(45,list(zip(y[0]))[1]))

it works.

( 2018-10-12 22:55:19 +0100 )edit

if i get values like p=[(232,), (343,), (543,), (23,)]

and

q=((232, 343, 543, 23),)

how can i will pass ?

i could not able to do without zip method, is there any other way to do?

( 2018-10-13 00:59:05 +0100 )edit

You can use the flatten function.

( 2018-10-13 11:14:30 +0100 )edit