# Python program to get average heights of distinct entries

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a set of heights there may be some duplicate entries as well. We have to find the average of distinct entries of these heights.

So, if the input is like heights = [96,25,83,96,33,83,24,25], then the output will be 52.2 because the unique elements are [96,25,83,33,24], so sum is 96 + 25 + 83 + 33 + 24 = 261, average is 261/5 = 52.2.

To solve this, we will follow these steps −

• h_set := a set from heights to remove duplicates

• return sum of h_set items / size of h_set set

## Example

Let us see the following implementation to get better understanding

def solve(heights):
h_set = set(heights)
return sum(h_set)/len(h_set)

heights = [96,25,83,96,33,83,24,25]
print(solve(heights))

## Input

[96,25,83,96,33,83,24,25]

## Output

52.2
Updated on 12-Oct-2021 07:09:11