Computer Programming I, Problems, Python

Find the Properties of list in Python - Part II

October 29, 2013

A couple of days ago I wrote an article about Finding the Average, Min, Max and Mode of a list in Python otherwise known as the Properties of list in Python. I thought it was very straight forward and a good educational experience - and it was. However, I was pointed out a few things I would like to clarify and official address. I apologize if I mislead anyone, please see this as an effort to make it up.

  1. The purpose of that article, this one, other articles and future posts is a recording of my experiences and educational drills. Posts are inspired by my day to day activities and questions I often get from students and co-workers. If you have any doubt as to my motives of writing and usefulness of my articles you are free to express them. Also, I suggested you read The Captain's Manifesto to get a better understanding of the Captain and what this blog is about.
  2. Naming a variable list is bad practice. I agree. That was a bad decision on my part. As s python coder, or any other language, variables should not carry the name of a datatype.  For more information I suggest you take a look at the Style Guide for Python Code.
  3. In the article, I failed to mention that we are re-writing some built in function. Python has the functions sum(), min() and max() built in. At there very basic form, they require an iterable datatype, which for now you may consider as a sequence, a list or a tuple. You can read more about these function on the Python Built-In Page.
  4. As of now, everything I write is write and tested under the assumption of Python 2.7.3. I will most likely not update to Python 3 until I must. I feel like the changes introduced in Python 3 are counter productive for Python. For my purposes and the educational element, Python 2.7.3 works better. If I wanted the constrains Python 3 introduced I would have picked another language for implementations on this blog. I actually may move on to introduce C and Java before I get to Python 3. Then again, some minor modification can be made to make every piece of code on here compatible with Python 3.
  5. One liner function such as this one are awesome and highly encouraged: (Thanks Jacob!)
 number, mode = max(dict((k, data.count(k)) for k in data).items(), key=lambda i: i[1]) 

Last but not least, Nico tried to post as a comment an updated version of the code combining all of these ideas, but the indentation got messed up. We all know that Python uses indentation for association of blocks of code, so it makes it a big deal. Here is a copy of the full Code to Compute Average, Min, Max and Mode of a List (or tuple)  in Python:

def average(my_list):
    return sum(my_list)/(len(my_list)*1.0)

def mode(my_list):
    a, b = max(dict((x, my_list.count(x)) for x in my_list).items(),
    key=lambda i: i[1])
    return a,b

def main():
	my_list = [3,4,1,20,102,3,5,67,39,10,1,4,34,1,6,107,99]
	print "The average element of the list is: " + str(average(my_list))
	print "The minimum value in the list is: "   + str(min(my_list))
	print "The maximum value in the list is: "   + str(max(my_list))

	mode_k = mode(my_list)
	print "The mode of the list is: "  + str(mode_k[0]) + " with the mode of: " + str(mode_k[1])
	print "The range of the list is: "   + str(max(my_list)-min(my_list))

if __name__ == "__main__":
	main()

This code will output the same result as we had before:

list_data_2_output
That's all! Happy hacking everyone.
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

You Might Also Like