On Python 3.4+ you can use statistics.mean()
l = [15, 18, 2, 36, 12, 78, 5, 6, 9] import statistics statistics.mean(l) # 20.11111111111111
On older versions of Python you can do
sum(l) / len(l)
On Python 2 you need to convert len
to a float to get float division
sum(l) / float(len(l))
There is no need to use reduce
. It is much slower and was removed in Python 3.
Related Posts:
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How to use filter, map, and reduce in Python 3
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Understanding slice notation
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- Finding the index of an item in a list
- Finding the index of an item in a list
- How to make a flat list out of a list of lists
- Removing duplicates in lists
- What does the list() function do in Python?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Writing a list to a file with Python
- if else in a list comprehension
- Print a list in reverse order with range()?
- Get unique values from a list in python [duplicate]
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- How to find all occurrences of an element in a list
- How to find all the indexes of a recurring item in a list?
- python – if not in list
- How do I concatenate two lists in Python?
- Replace values in list using Python
- Syntax behind sorted(key=lambda: …)
- Union of two lists in Python
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- Writing a list to a file with Python
- Python: Get the first character of the first string in a list?
- Understanding slice notation
- How do I find the duplicates in a list and create another list with them?
- Add list to set?
- How to remove an element from a list by index
- Apply function to each element of a list
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- Remove all occurrences of a value from a list?
- Python return list from function
- TypeError: list indices must be integers or slices, not str
- In Python, when to use a Dictionary, List or Set?
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- Removing Punctuation From Python List Items
- Check if two unordered lists are equal
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How can I compare two lists in python and return matches
- How can I get list of values from dict?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to append multiple values to a list in Python
- How do you split a list into evenly sized chunks?
- How to read a text file into a list or an array with Python
- Map to List error: Series object not callable
- How to return dictionary keys as a list in Python?
- How can I create an array/list of dictionaries in python?
- How to sort a list/tuple of lists/tuples by the element at a given index?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- How to multiply all integers inside list
- Get the cartesian product of a series of lists?
- What does the “x for x in” syntax mean?
- How to remove square brackets from list in Python?
- numpy-equivalent of list.pop?
- Create nice column output in python
- TypeError: only integer arrays with one element can be converted to an index 3
- Logical indexing with lists
- How does str(list) work?
- convert csv file to list of dictionaries
- creating a reverse method for a python list from scratch
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- Turn a single number into single digits Python
- How to create a list of objects?
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- Get difference between two lists
- Circular list iterator in Python
- How is Python’s List Implemented?
- NameError: name ‘reduce’ is not defined in Python
- Sorting list based on values from another list
- Create a list with initial capacity in Python
- Python initializing a list of lists
- Does Python have an immutable list?