Python all any built-in function

I have been using Python for a long time, still learning lot of simple and new things. Recently I came to know about two built-in functions named all and any . Today while I was solving a hackerrank problem , I used both functions which makes the code a bit nicer, in my opinion. The functions compares two singly linked lists and if they are equal, returns 1, otherwise returns 0. Here is my code - def compare_lists(llist1, llist2): while all([llist1, llist2]): if != return 0 llist1 = llist2 = if any([llist1, llist2]): return 0 return 1 Sometimes, this kind of simple thing brings joy to me. Let me know your thoughts. :)

Create 2D Array using List in Python

If for some reason you want to create a two dimensional array in Python and don't want to use external packages like numpy etc., the most obvious thing is to write a code like this : >>> n = 3 >>> m = 4 >>> ara = [[0] * m] * n >>> ara [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] But there is a problem. Check the code below- >>> ara[0][0] = 1 >>> ara [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]] Actually, when you do [0] * m, it returns a reference to a list and when you multiply it with n, the same reference is duplicated. Hence you see the change in a way that you didn't expect. The right way to do it is to append [0] * m, n times in the array. You can code it in multiple ways. My preferred way is: >>> ara = [[0] * m for _ in range(n)]

Table Driven Unit Test in Python

Now days, table driven tests are pretty much industry standard. In my workplace, we use table driven tests when we write unit tests (in golang though). Here I shall share a simple code example using pytest that shows how to write table driven tests in Python. In table driven test, what you need to do is, to gather all the tests cases together in a single table. We can use dictionary for each test case and a list to store all the test cases. Instead of discussing it further, let me show you an example : def average(L): if not L: return None return sum(L)/len(L) def test_average(): test_cases = [ { "name": "simple case 1", "input": [1, 2, 3], "expected": 2.0 }, { "name": "simple case 2", "input": [1, 2, 3, 4], "expected": 2.5 }, { "name": "list w

global and nonlocal variable in Python

Most of us are already familiar with global variables in Python. If we declare those variables in a module, the functions inside that module (can read python file or .py file) can access the variable. For example, check the code below : x = 5 def myfnc(): print("inside myfnc", x) def myfnc2(): print("inside myfnc2", x) myfnc2() myfnc() It will print :  inside myfnc 5 inside myfnc2 5 If you change your code like this : x = 5 def myfnc(): print("inside myfnc", x) def myfnc2(): print("inside myfnc2", x) x = 10 print("x = ", x) myfnc2() myfnc() You will get an error :   File "", line 6, in myfnc2     print("inside myfnc2", x) UnboundLocalError: local variable 'x' referenced before assignment The moment you wrote x = 10, Python assume that x is a local variable, and inside the print function, it is giving this error. Because local variables are determi

Python Code Measure Time

How to measure time of a Python code snippet? We can do it using the timeit module . It has a function called timeit that runs a particular code snippet for N times (one million by default) and gives you the runtime. Today I just used it for a small purpose and thought of writing a note here. >>> from timeit import timeit >>> timeit('"c" >= "a" and "c" <= "z"') 0.06282545300200582 >>> timeit('"c".isalpha()') 0.06563570606522262 Here I compared the performance between two snippets : one uses isalpha() to determine if a (lowercase) character is an alphabet or not, another just compares using operators. There is no noticeable performance difference. Please go through the module doc to get some more ideas of how to use the timeit module , if will often come handy when you quickly want to measure running time of different Python code.

Code School Python Courses

As Python is getting more and more popular and becoming the de facto standard for starting to learn programming, we can see increasing amount of online content to learn Python. Last week got an email form Code School team to check out their couple of new Python courses. I spent around 30 minutes watching the videos, checkout out the exercises. The exercises are good, and can be done online. They also prepared the videos with care. But one thing that bothers me, the lecture is kind of robotic. I didn't feel anything special, in fact didn't feel the connection to the teacher that I used to feel in other courses in Coursera and Udacity. Anyway, Python beginners can check those courses: 1. Try Python 2. Flying through Python