# Finding Median and Mode using reduce

Page 1 of 1

## 4 Replies - 2376 Views - Last Post: 04 September 2019 - 08:22 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=412811&amp;s=99e1d8f27782a2dcd3630698428c8d7d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 uwl

Reputation: 0
• Posts: 21
• Joined: 02-August 18

# Finding Median and Mode using reduce

Posted 22 September 2018 - 02:56 PM

I am trying to find the Median and the Mode using Python's reduce() function.

```from functools import reduce

def sumAB(a, B)/>:
c = a + b
return c

def meanMedianMode(numbers):

calculate = {
'mean': reduce(sumAB, numbers) / len(numbers),
# count how many numbers, add 1 then divide by 2
'median': reduce(),
# count number occurs most often
'mode': reduce()}

return calculate

mmm_dict = meanMedianMode([1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10])
print(mmm_dict)

```

How do I do this, do I need to create another function?

Can someone walk me through accomplishing this?

This post has been edited by uwl: 22 September 2018 - 02:58 PM

Is This A Good Question/Topic? 0

## Replies To: Finding Median and Mode using reduce

### #2 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: Finding Median and Mode using reduce

Posted 22 September 2018 - 03:11 PM

Why use reduce() ?

Mean is ok, but simpler is: sum(numbers)/len(numbers)
Median is the middle value when numbers are sorted >> sort and use list index.
Mode (do you mead the most frequent number..?) >> itertools.groupby or collections.Counter or home-build loop.

### #3 wbyker

Reputation: 0
• Posts: 1
• Joined: 25-August 19

## Re: Finding Median and Mode using reduce

Posted 25 August 2019 - 03:47 PM

i am doing this same thing for the lambda school, the best results I have came up with are in this video link on youtube.com
do you still have a copy of how you solved this?

### #4 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: Finding Median and Mode using reduce

Posted 03 September 2019 - 09:06 AM

@wbyker: You know, asking for code is not in compliance with the rules here.

I'll be happy to help. Show your code and explain the associated problem; providing a set of sample data and expected output is also helpful.

### #5 baavgai

• Dreaming Coder

Reputation: 7500
• Posts: 15,541
• Joined: 16-October 07

## Re: Finding Median and Mode using reduce

Posted 04 September 2019 - 08:22 AM

Hmm... len seems like cheating. This actually works:
```xs = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
print((lambda tot_sz: tot_sz[0] / tot_sz[1])(reduce(lambda acc, x: ((acc[0] + x), (acc[1] + 1)), xs, (0, 0))))

```

Because, honestly, if you were reducing you'd probably be counting. It's a rather functional thing to do.

You can, theoretically, use reduce for most everything lispy:
```print(reduce(lambda x, y: x / y, reduce(lambda acc, x: ((acc[0] + x), (acc[1] + 1)), xs, (0, 0))))

```

Along the same lines, you could build a dict for that median. Though you won't be able to use a lambda, you'll need a spare function for that. You could actually reduce all the way there in other languages, but I don't know that python has the syntax for it in a lambda.