1

Let's say for example one wants to iterate through 3 list containing the same number of items. How would I be able to compare the items of same index and put the median of those in a new list called median list?

example output:

list_1 = [1,2,3]
list_2 = [1,3,5]
list_3 = [2,4,6]

median_list = [1, 3, 5]
falsetru
  • 357,413
  • 63
  • 732
  • 636
star
  • 179
  • 1
  • 1
  • 6
  • 1
    you need to do something by yourself. This is not a place to crowdsource your problems for free. – Salvador Dali Nov 15 '14 at 23:57
  • I'm new to coding and have been attempting to try since yesterday. If you can't help, thanks anyway. – star Nov 15 '14 at 23:58
  • See http://stackoverflow.com/questions/24101524/finding-median-of-list-in-python for ways of finding the median in general – Stuart Nov 16 '14 at 00:10

1 Answers1

2

Using zip and list comprehension:

>>> list_1 = [1,2,3]
>>> list_2 = [1,3,5]
>>> list_3 = [2,4,6]

>>> zip(list_1, list_2, list_3)  # To make 1st items pair, 2nd items pairs, ...
[(1, 1, 2), (2, 3, 4), (3, 5, 6)]

>>> [sorted(xs)[1] for xs in zip(list_1, list_2, list_3)]
[1, 3, 5]

If you are using Python 3.4+, you can use statistics.median:

>>> [statistics.median(xs) for xs in zip(list_1, list_2, list_3)]
[1, 3, 5]
falsetru
  • 357,413
  • 63
  • 732
  • 636