How do you find the range of a list in python?

I have a set of data that is in a list. I am not sure how to make a function which can take the range of that data and return the min and max values in a tuple.

data:

[1,3,4,463,2,3,6,8,9,4,254,6,72]

my code at the moment:

def getrange[data]:
    result=[]
    if i,c in data:
        range1 = min[data]
        range2 = max[data]
        result.append[range1, range2]
    return result 

asked Aug 22, 2015 at 4:15

NinjaNinja

611 gold badge1 silver badge7 bronze badges

4

This is a very straight forward question and you're very close. If what I have below isn't correct, then please edit your question to reflect what you would like.

Try this.

def minmax[val_list]:
    min_val = min[val_list]
    max_val = max[val_list]

    return [min_val, max_val]

Semantics

I have a set of data that is in a list.

Be careful here, you're using python terms in a contradictory manner. In python, there are both sets and lists. I could tell you meant list here but you could confuse people in the future. Remember, in python sets, tuples, and lists are all different from one another.

Here are the differences [taken from BlackJack's comment below]

Data Type | Immutable | Ordered | Unique Values
===============================================
  lists   |    no     |   yes   |      no
  tuples  |    yes    |   yes   |      no
   sets   |    no     |   no    |      yes

Immutable - the data type can't be changed after instantiation.

Ordered - the order of the elements within the data type are persistent.

Unique Values - the data type cannot have repeated values.

Mike Holt

4,3421 gold badge15 silver badges23 bronze badges

answered Aug 22, 2015 at 4:23

Austin AAustin A

2,8606 gold badges25 silver badges41 bronze badges

7

If You Are Looking To Get The Range Of The Numbers, You Can Use:

def getrange[numbers]:
    return max[numbers] - min[numbers]

I've Also Constructed This Code That You Can Use In Finding Averages:

def average[numbers, type=None]:
import statistics
try:
    statistics.mean[numbers]
except:
    raise RuntimeError['An Error Has Occured: List Not Specified [0018]']
if type == 'mean':
    return statistics.mean[numbers]
elif type == 'mode':
    return statistics.mode[numbers]
elif type == 'median':
    return statistics.median[numbers]
elif type == 'min':
    return min[numbers]
elif type == 'max':
    return max[numbers]
elif type == 'range':
    return max[numbers] - min[numbers]
elif type == None:
    return average[numbers, 'mean']
else:
    raise RuntimeError['An Error Has Occured: You Entered An Invalid Operation [0003]']

All You Need To Do Is Type average[[1, 2, 3]] To Get The Mean Average For 1, 2 And 3. For Other Commands, Do average[[1, 2, 3, 'median'] And This Will Give The Median Of The Numbers. You Can Change median to: mean, mode, median, min, max and range

answered Nov 4, 2017 at 3:37

Richie BendallRichie Bendall

6,2263 gold badges35 silver badges51 bronze badges

1

I like NumPy's percentile function for the ability to get multiple percentiles at once:

import numpy as np
print np.percentile[[1,3,4,463,2,3,6,8,9,4,254,6,72], [0, 100]]

Output:

[   1.  463.]

[The minimum is the 0 % percentile; and the maximum is the 100 % percentile.]

If you really need the result in a tuple, you can easily wrap it with tuple[...].

answered Aug 22, 2015 at 9:23

FalkoFalko

16.4k13 gold badges54 silver badges100 bronze badges

I just did this:

>>>data = [1,3,4,463,2,3,6,8,9,4,254,6,72]
>>>min_val = min[data]
>>>max_val = max [data]
>>>range_data = [min_val, max_val]
>>>print[range_data]
[1, 463]

answered May 26, 2020 at 16:37

1

How do you range a list in Python?

In Python 2, we have range[] and xrange[] functions to produce a sequence of numbers. In Python 3 xrange[] is renamed to range[] and original range[] function was removed. ... Summary..

How do you find the range of a list?

The range is the difference between the smallest and highest numbers in a list or set. To find the range, first put all the numbers in order. Then subtract [take away] the lowest number from the highest. The answer gives you the range of the list.

What is range [] in Python?

Python range[] Function The range[] function returns a sequence of numbers, starting from 0 by default, and increments by 1 [by default], and stops before a specified number.

Chủ Đề