How to Square a List in Python

In this Python tutorial, I will show you how to square a list in Python with examples.

Squaring a number simply means multiplying that number by itself. So, for example, the square of 2 is 4 (2 * 2), the square of 3 is 9 (3 * 3), and so on.

Square a list of numbers in Python

Now, let us check different ways to square a list of numbers in Python.

There are several ways to square a list of numbers in Python.

Method 1: Using a for loop

You can use a for loop to iterate over the list and square each number in Python. Here’s how:

``````my_list = [1, 2, 3, 4, 5]
squared_list = []

for number in my_list:
squared_list.append(number**2)

print(squared_list)``````

In this code, `number**2` is where the squaring happens. The `**` is Python’s exponentiation operator.

Output:

``[1, 4, 9, 16, 25]``

You can see the output when I executed the Python program.

Method 2: Using list comprehension

List comprehension is a concise way to create lists based on existing lists. Here’s how to use it to square a list:

``````my_list = [1, 2, 3, 4, 5]
squared_list = [number**2 for number in my_list]

print(squared_list)``````

Output:

``[1, 4, 9, 16, 25]``

As you can see, this does the same thing as the for loop example, but in a more concise way.

Check out the below output of the code.

Method 3: Using the map function

The map function in Python applies a given function to each item of an iterable (like a list) and returns a list of the results.

``````my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))

print(squared_list)``````

Output:

``[1, 4, 9, 16, 25]``

In this code, `lambda x: x**2` is a small anonymous function that squares its input.

READ:  Merge Dictionaries in Python (8 different methods)

Another example of having negative numbers:

``````my_list = [-1, -2, -3, -4, -5]
squared_list = list(map(lambda x: x**2, my_list))

print(squared_list)``````

Output:

``[1, 4, 9, 16, 25]``

In this example, the Python list contains negative numbers. When a negative number is squared, the result is positive.

Below you can check the Python code execution.

How to square a number in Python

Now, let us look at a simple example of how to square a number in Python.

In Python, you can square a number using the exponentiation operator `**`. This operator raises the number on its left to the power of the number on its right. Here’s the syntax:

``number_squared = number ** 2``

And here’s a simple example:

``````number = 7
number_squared = number ** 2
print(number_squared)``````

When you run this code, you’ll get the output:

``49``

In this example, the number 7 is squared, so the result is 49. You can replace `7` with any number you want to square.

If you want to create a reusable function to square a number, you can do that like this:

``````def square(number):
return number ** 2

print(square(7))``````

When you run this code, you’ll get the same output:

``49``

In this version of the code, the `square` function takes one argument (the number you want to square), squares it, and returns the result. You can call this function with any number you want to square.

Conclusion

In this tutorial, we learned about squaring a number in Python. And also we saw different ways how to square a list of numbers in Python.

You may like the following Python tutorials: