How to Square in Python: List Values

If you have numbers in a Python list, you can make a corresponding list of their squares. Squaring multiplies each number by itself, and a list comprehension collects those results in a new list.

I’ll show how to build that new list and how the result types differ across Python operations and NumPy.

TL;DR: Square Each List Value with a Comprehension

Use a comprehension with ** to return a new list containing the square of each input value.

  • Choose an index loop when you intend to replace values in the original list.
  • Use NumPy when the data already belongs to an array workflow.

What Is Squaring a Python List?

Squaring multiplies each number by itself. Apply the operation to each item to get a list of corresponding results.

I included a negative integer and a float in the example to check both signs and fractional output.

InputSquareWhat to note
393 × 3
-39A negative base needs parentheses in a power expression
2.56.25The result is a float

Use ** for exponentiation. The caret, ^, is bitwise XOR for integers. Parenthesize a negative base to square it as a negative value.

How to Square a List in Python Step by Step

I use a comprehension here because it returned each square without changing the input list.

Step 1: Build a New List with a Comprehension

The expression before for calculates each result. This function accepts any iterable whose values support exponentiation. I ran python3 square_list_demo.py and got [9, 0, 4, 6.25], while the printed input remained [-3, 0, 2, 2.5].

def square_values(values):
    return [value ** 2 for value in values]

numbers = [-3, 0, 2, 2.5]
squares = square_values(numbers)
print("Input:", numbers)
print("Squares:", squares)
print("Input after:", numbers)
print("Empty input:", square_values([]))

Run the saved example with this command.

python3 square_list_demo.py
Input: [-3, 0, 2, 2.5]
Squares: [9, 0, 4, 6.25]
Input after: [-3, 0, 2, 2.5]
Empty input: []
Terminal output listing input and squared values from a Python list comprehension
The output shows the source list, its squares, and the result for an empty list.

The empty input returns [] without a special branch. Repeated values stay in their positions, with a square for each occurrence.

Return the list from a function when callers need its values. print() displays text instead. Add a condition after for when only selected values should produce results.

Step 2: Update the Existing List with a Loop

Assign by index when later code should see the original list’s values replaced.

numbers = [1, -2, 3]
for index, number in enumerate(numbers):
    numbers[index] = number ** 2
print("Updated:", numbers)

Run the loop example.

python3 square_list_loop.py
Updated: [1, 4, 9]
Terminal output showing the updated list after an index-based loop
The loop replaces each value in the list.

enumerate() provides the index used by the assignment. If a function builds a new list with append(), place return after the loop so it processes every value.

Step 3: Compare Python and NumPy Results

Use ** or built-in pow() for Python values. math.pow() converts its arguments to floats, while numpy.square() works with array-shaped data and retains its dtype.

import math
import numpy as np

values = [2, 1.5, 100, -3]
print("**:", [value ** 2 for value in values])
print("built-in pow:", [pow(value, 2) for value in values])
print("math.pow:", [math.pow(value, 2) for value in values])
print("NumPy:", np.square(np.array(values)).tolist())
large = 2**53 + 1
print("exact integer square:", large**2)
print("math.pow square:", math.pow(large, 2))
small = np.array([100], dtype=np.int8)
print("int8 square:", np.square(small).tolist())

Run this comparison when you need to check the result types and array behavior.

python3 compare_square_methods.py
**: [4, 2.25, 10000, 9]
built-in pow: [4, 2.25, 10000, 9]
math.pow: [4.0, 2.25, 10000.0, 9.0]
NumPy square as list: [4.0, 2.25, 10000.0, 9.0]
exact integer square: 81129638414606699710187514626049
math.pow square: 8.112963841460668e+31
int8 square: [16]
Terminal output comparing exponentiation, pow(), math.pow(), and NumPy square
The comparison shows integer and float outputs, plus a narrow NumPy integer result.

For integer inputs, ** and pow() keep integer results. math.pow() converts to floating point, which can lose precision for large integers. The int8 result wraps because 10000 exceeds its range.

NumPy returns an array here, and tolist() converts it to a Python list. Use it when the surrounding work already uses arrays.

Common Errors and Edge Cases When Squaring a Python List

Check each input’s type and the output representation when values may not be ordinary integers.

CaseSymptomFix
Empty listThe result is []Use the comprehension without a special branch
String in numeric valuesTypeError: unsupported operand type(s) for ** or pow(): ‘str’ and ‘int’Validate or convert accepted values first
Negative base-3 ** 2 gives -9Write (-3) ** 2 for a positive square
NumPy int8 value 100The result is [16] after overflowChoose a dtype that can hold the square
Nested listA row cannot be raised to a powerUse a comprehension inside another comprehension

I passed a string in the list and received this TypeError. The comprehension stops at that value instead of skipping it.

numbers = [1, "two", 3]
try:
    squares = [number ** 2 for number in numbers]
except TypeError as error:
    print(f"{type(error).__name__}: {error}")

Run the example to see the error message.

python3 square_list_type_error.py
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Terminal output showing a TypeError for a string in numeric values
The message identifies the string and exponentiation as incompatible operands.

Convert only accepted string formats. Construct decimal.Decimal from text for base-ten precision. For nested lists, use an inner comprehension for each row.

Conclusion

Choose the operation by the result your next step expects, then check its numeric type and range.

For related examples, see AskPython’s list-comprehension guide and list division guide. The Python expression reference documents power operators, and the NumPy square reference describes array behavior.

FAQ

These answers distinguish similar Python operations and result types.

Is pow() built into Python?

Yes. pow(base, exponent) is built in and does not need an import.

Why does math.pow() return a float?

math.pow() converts its arguments to float. Use ** or pow() when exact integer powers matter.

Can numpy.square() take a Python list?

Yes. It accepts array-like input and returns an array. Use tolist() if the next code needs a Python list.

Jasmine Massey
Jasmine Massey
Articles: 7