#!/usr/bin/python2 def commonInLists(listOfLists): # Input: List of lists # Output: Single list of common elements # Must take unknown number of lists if len(listOfLists) == 0: raise Exception('No.') elif len(listOfLists) == 1: return listOfLists[0] elif len(listOfLists) == 2: return list(set(listOfLists[0]).intersection(listOfLists[1])) elif len(listOfLists) > 2: result = set(listOfLists[0]).intersection(listOfLists[1]) for i in range(2, len(listOfLists)): result = result.intersection(listOfLists[i]) return result else: raise Exception('How did I get here?') a = ['1', '2', '3', '4', '5'] b = ['2', '3', '4', '5', '6'] c = ['3', '4', '5', '6', '7'] d = ['4', '5', '6', '7', '8'] foo = [a, b, c, d] print commonInLists(foo)