def avg_position(positions): assert type(positions) in (list, tuple) total_value = 0 total_quantity = 0 for position in positions: quantity, price = position total_quantity += quantity total_value += quantity / price return total_quantity / total_value