# 5 Best Ways to Create a List of Tuples with Number and Its Square in Python

Rate this post

π‘ Problem Formulation: You need to generate a list of tuples in Python, where each tuple contains a number and its square. For instance, given an input range of numbers from 1 to 5, the desired output would be a list: [(1,1), (2, 4), (3, 9), (4, 16), (5, 25)]. This article explores five different methods to achieve this task, focusing on both clarity and efficiency.

## Method 1: Using a For Loop

Using a traditional `for` loop is one of the most straightforward methods for creating a list of tuples, where you iterate over a sequence of numbers, calculate the square of each, and append a tuple to the list.

Here’s an example:

```list_of_tuples = []
for num in range(1, 6):
list_of_tuples.append((num, num**2))```

Output:

`[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]`

This snippet uses a `for` loop to iterate through the numbers 1 to 5. For each number, it appends a tuple to the list, where the first element is the number itself, and the second element is its square.

## Method 2: Using List Comprehension

List comprehensions in Python provide a concise way to create lists. A list comprehension consists of brackets containing an expression followed by a `for` clause, then zero or more `for` or `if` clauses.

Here’s an example:

`list_of_tuples = [(num, num**2) for num in range(1, 6)]`

Output:

`[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]`

In this code snippet, a list comprehension is used to create the desired list of tuples in a single, readable line. Each tuple is generated inline, making this a compact and pythonic solution.

## Method 3: Using the `map()` Function

The `map()` function applies a given function to each item of an iterable (like a list) and returns a list of the results. It can be used to pair elements with their squares efficiently.

Here’s an example:

`list_of_tuples = list(map(lambda num: (num, num**2), range(1, 6)))`

Output:

`[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]`

The `map()` function is used here with a lambda function that creates the tuple. The resulting map object is then converted back into a list. This method emphasizes functional programming techniques in Python.

## Method 4: Using the `zip()` Function

The `zip()` function is used to map the similar index of multiple containers so that they can be used as a single entity. We can use it to zip a range with its squared values.

Here’s an example:

```numbers = range(1, 6)
squares = [num**2 for num in numbers]
list_of_tuples = list(zip(numbers, squares))```

Output:

`[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]`

This code snippet zips two lists: one of numbers and one of the squared numbers. The `zip()` function pairs corresponding elements from each list, creating the desired tuples.

## Bonus One-Liner Method 5: Using a Generator Expression with `tuple()`

A generator expression is similar to a list comprehension, but it does not create the list in memory. Instead, you can use the `tuple()` function to directly generate tuple pairs.

Here’s an example:

`list_of_tuples = list(tuple((num, num**2)) for num in range(1, 6))`

Output:

`[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]`

The generator expression within the `tuple()` function generates each tuple on demand, and `list()` is used to create a list of these tuples. This method is both memory efficient and concise.

## Summary/Discussion

• Method 1: For Loop. Itβs straightforward and easy for beginners to understand. However, it might not be the most pythonic or efficient solution for large datasets.
• Method 2: List Comprehension. This method is compact and pythonic, ideal for writing concise code. It’s generally faster than a for loop, but readability might suffer with complex expressions.
• Method 3: Using `map()` Function. This method is functional and clean, especially suitable for applying the same operation to each element. However, it may be less intuitive for those not familiar with functional programming.
• Method 4: Using `zip()` Function. It’s a creative use of `zip()` and is useful when operating on two lists in parallel. The downside is that it requires the creation of an intermediate list for the squared numbers.
• Bonus Method 5: Generator Expression. This method is memory efficient and elegant for larger data sets. However, the use of generator expressions may be a bit advanced for beginners.