Python program to get all unique combinations of two Lists Last Updated : 25 Nov, 2024 Summarize Suggest changes Share Like Article Like Report The goal is to combine each item from first list with each item from second list in every possible unique way. If we want to get all possible combinations from two lists. Python’s itertools library has a function called a product that makes it easy to generate combinations of items from multiple lists. Python import itertools a = ["a", "b"] b = [1, 2] #It generate all possible combinations of elements from both lists # Convert the result into a list of tuples combinations = list(itertools.product(a, b)) print(combinations) Output[('a', 1), ('a', 2), ('b', 1), ('b', 2)] itertools.product automatically creates the combinations of elements from both lists. We use list() to convert the result into a list of tuples.There are other methods which can be used that are both fast and efficient :Table of ContentUsing itertools.permutationsUsing a Nested Loop Using List ComprehensionUsing itertools.permutations itertools.permutations function creates all possible permutations of a given list. We can combine this with zip to create unique combinations of two lists. Python import itertools a = ["a", "b"] b = [1, 2] # Generate all 2-length permuations of pairs by zipping lists combinations = list(itertools.permutations(zip(a, b), 2)) print(combinations) Output[(('a', 1), ('b', 2)), (('b', 2), ('a', 1))] The zip(a, b) combines the two lists element-wise into pairs, and itertools.permutations then creates all possible orders of these pairs. This method returns all unique permutations, including reversed combinations.Using a Nested Loop We can get all combinations is by using two loops. We can loop through each element in both lists and combine them. Python X = ["a", "b"] Y = [1, 2] # Initialize an empty list combinations = [] # Loop through each element in List_1 for x in X: # Loop through each element in List_2 for y in Y: combinations.append((x, y)) print(combinations) Output[('a', 1), ('a', 2), ('b', 1), ('b', 2)] Using List ComprehensionIf we have to write cleaner and shorter code, we can use list comprehension to achieve the same result. This is essentially the same thing as using loops but in a single line. It’s a concise way to combine elements. Python X = ["a", "b"] Y = [1, 2] #list combination of all possible elemenst of two lists(X and Y) combinations = [(x, y) for x in X for y in Y] print(combinations) Output[('a', 1), ('a', 2), ('b', 1), ('b', 2)] Advertise with us Next Article Python | Program to count duplicates in a list of tuples V vanshgaur14866 Follow Similar Reads Python List and Tuple Combination Programs Lists and tuples are two of the most commonly used data structures in Python. While lists are mutable and allow modifications, tuples are immutable and provide a stable structure for storing data. This article explores various programs related to list and tuple combinations, covering topics like:Sor 6 min read Python | Program to count duplicates in a list of tuples Given a list of tuples, write a Python program to check if an element of the list has duplicates. If duplicates exist, print the number of occurrences of each duplicate tuple, otherwise print "No Duplicates". Examples: Input : [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')] Output : ('a 6 min read Python - Print all common elements of two lists Finding common elements between two lists is a common operation in Python, especially in data comparison tasks. Python provides multiple ways to achieve this, from basic loops to set operations. Let's see how we can print all the common elements of two listsUsing Set Intersection (Most Efficient)The 3 min read Python - All Possible unique K size combinations till N Sometimes, while working with Python domain, we can have a problem in which we need to produce various combination of elements. This can be K sized unique combinations till N. This problem can have application in data domains and school programming. Let's discuss certain ways in which this task can 4 min read Python - Get all numbers combinations in list Sometimes, while working with Python lists, we can have a problem in which we need to concatenate each number with other create new number. This kind of problem is peculiar but can have application in many domains such as day-day programming and gaming. Let's discuss certain ways in which this task 3 min read Python - All pair combinations of 2 tuples Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let's discuss certain ways in which this task can be performed. Input : test_tuple1 6 min read Article Tags : Python Python Programs Python list-programs Python-itertools Practice Tags : python Like