Squaring List Elements in Python

Squaring List Elements

A list is one of the data types in Python, just like a tuple, set, or dictionary. These datatypes – List, Tuple, set or dictionary- are used to store data collections. Lists are used to store multiple elements, which can be stored in one single variable.

In this article, we will look at how squaring list elements work. We will look at the article to square the values inside the list.

What is a List?

In lists, we can store the same value again, which means duplicate values. Lists are used to store multiple items, which can be stored in one single variable. A list looks like this: [‘a’, ‘1’, ‘b’, 2]. List values are written inside the square brackets, [].

Syntax

list = [ list-elem1, list-elem2, ..., list-elem]

Let’s take a look at an example.

list_nums= [10, 20 , 30 , 40, 50]
print(list_nums)

for i, v in enumerate(list_nums):
    print(f"At index {i}, value is {v}")
  • assigned a list containing values to the variable list_nums= [10, 20, 30, 40, 50]
  • firstly, we normally print the output
  • secondly, we print a list with its indexes and values. We are using enumerate() function, which will help us keep track of the index and elements.
Python List
Python List

In the output, we are getting a normal list of values, and with the help of for loop, we looped through the list using enumerate() function helped us to get the index and the elements.

You may check: How to Divide Each Element in a List in Python?


Squaring List Elements

In the above example, we saw how to print the list and its elements. Here we will print the list with the square value. For example, the square of 2 is 22 = 2 × 2 = 4.

Example 1: Using ** Power Operator

** double asterisk or power operator is used between the base and exponent values like(2**2)=(22). Double asterisk ** can be for squaring list elements.

list_nums= [1, 2, 3, 4, 5]

for i, v in enumerate(list_nums):
   list_nums[i] = v**2

print(list_nums)
  • assigned a list [1, 2, 3, 4, 5] values to the variable list_nums
  • as we have looped before in the above example, here also, we are using enumerate() function where it will help us in getting the index and the values
  • variable “i” is for the indices, and “v” is for the values of the list(list_nums). List Indexes start from 0.
  • Initially, list_nums[i] = list_nums[0] because i = 0, storing the value calculated by v**2, which is (1**2), so at the first index, we get the value 1 only.
Squaring List Elements
Squaring List Elements

In the result, we are getting square values list. The squared values are [1, 4, 9, 16, 25].

Let’s see an example with List Comprehension.

list_nums=[1, 2, 3, 4, 5]
print("Original list:",list_nums)
list_nums= [i**2 for i in list_nums]
print("Square elements:",list_nums)
  • assigned a list: [1, 2, 3, 4, 5] values to the variable list_nums
  • using list comprehension produces the same result as we saw using for loop, but with list comprehension, it became a little more concise and neat.
Square List Comp

In the result, we are getting square values list. The updated list with square values is [1, 4, 9, 16, 25].

Example 2: Using pow() function

pow() function is basically from the math module provided by python. The pow() function returns the value of v to the power of n (vn). We will use this function for squaring list elements in this article.

Pow Function
pow() Function
import math

list_nums= [5, 4, 3, 2, 1]
print("Original List:", list_nums)
list_nums= [math.pow(i, 2) for i in list_nums]
print("Square Elements:",list_nums)
  • import the math module, which will provide us with pow() function
  • assigning a list containing 5, 4, 3, 2, and 1 values to the variable list_nums
  • for comparison printing the original list first, then the updated list.
  • then in the nums variable only, we assigned the squared values using list comprehension, here, inside the square brackets, we use math.pow() function takes 3 arguments in total, but we are giving 2 to get the desired result.
  • the first argument in math.pow() function is the element on which we will apply the squaring functionality, and the other is the power we provided 2 here for squaring the element.
  • lastly, printing the result which will get stored in the variable list_nums.
Pow Square Elems
Pow Square Elems

The first result is the original list: [5, 4, 3, 2, 1]. Secondly, the output is for the squared elements: [25.0, 16.0, 9.0, 4.0, 1.0]. As we can see in the second result that we are getting the float values because this method converts the arguments into a float value and returns the floating point number.

Example 3: Using NumPy’s square() function

As we have seen two methods above, this is the third method where we will be using numpy square method, which will help us square list elements. numpy.square() returns the element-wise square.

To use numpy.square(), we have to first install the numpy package to use its functionalities.

pip install numpy
import numpy

list_nums= [1, 2, 3, 4, 5]
print("Original List:", list_nums)

list_nums= numpy.square(list_nums)
print("Square Elements:",list_nums)
  1. import numpy package for using its methods
  2. assigned a list: [1, 2, 3, 4, 5] values to the variable list_nums
  3. then, we use numpy.square() method to square the elements, which took the nums list as an argument
  4. lastly, printing the result will be stored in the list_nums variable.
Numpy Square Elems

In the output, we printed the original list first, and then it printed the list with square numbers.

You may also check: 5 Easy Ways To Extract Elements From A Python List


Conclusion

In this article, we saw how to square the elements of the list. We saw the simple example of a list and how we can create one and print the indexes of the list using enumerate() function. We used the ** (double asterisk) power operator, math.pow() function, and numpy.square() method to perform the squaring of elements in the list.

You can refer to Python Documentation on Python lists.