Return All Possible Pairs of List Elements in Python (2 Examples)

In this tutorial, you’ll learn how to return all possible pairs of elements from a given list in Python. Generating pairs can be useful in various scenarios, such as finding combinations or permutations of elements. We’ll explore different methods to achieve this.

The table of contents is structured as follows:

Let’s get started!

Initializing a Sample List

Firstly, let’s initialize a sample list:

# Initializing a sample list
my_list = [1, 2, 3, 4]

This creates a list, my_list, with some elements.

Example 1: Extract Pairs in a List Using Nested Loops

One way to generate all possible pairs is by using nested loops to iterate over the list. Here’s an example:

# Generate all possible pairs using nested loops
pairs = []
for i in range(len(my_list)):
    for j in range(i + 1, len(my_list)):
        pairs.append((my_list[i], my_list[j]))

In this example, we use two nested for loops. The outer loop iterates over the elements of the list from the first element to the second-to-last element. The inner loop iterates over the elements following the current element of the outer loop.

We form pairs by combining each element of the outer loop with each element of the inner loop. The pairs are stored in a list called pairs.

Let’s print the pairs generated using nested loops:

print(pairs)
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

The output shows all possible pairs of elements from the list.

Example 2: Extract Pairs in a List Using itertools.combinations()

Python’s itertools module provides a handy function called combinations() that allows us to generate all possible pairs from a list. Here’s how we can use it:

import itertools
 
# Generate all possible pairs
pairs = list(itertools.combinations(my_list, 2))

In this example, we import the itertools module and use the combinations() function. We pass in the list my_list as the first argument and specify the size of the combinations (in this case, 2) as the second argument.

The combinations() function returns an iterator, so we convert it to a list using list() to easily access and print the pairs. Let’s print the pairs!

print(pairs)
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

As you can see, once again, all possible pairs of elements from the list are generated via this method as well.

Video, Further Resources & Summary

In this tutorial, we explored different methods to generate all possible pairs of elements from a given list in Python. We learned how to use nested loops to iterate over the list and generate pairs, as well as how to utilize the itertools module to achieve the same result. These techniques can be helpful in various scenarios, such as finding combinations or permutations of elements in a list.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added

soon.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Now you have the knowledge and techniques to generate all possible pairs of elements from a list in Python. Happy coding!

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top