Python program to create a list centered on zero

Creating a list centered on zero involves generating a sequence of numbers where zero is positioned in the middle of the list. While the size of the list can be customized, it is often preferred to use an odd number to ensure symmetrical distribution of elements around zero.

In this article, we will discuss different approaches to creating a list centered on zero using Python programming.

Approach

We can follow the below steps to create a centered list:

  • Determine the desired size of the list. Let's call this value n.

  • If n is an odd number, it is already suitable for creating a centered list. If n is an even number, we need to adjust it to the next odd number to ensure that zero is at the center.

  • Calculate the half-size of the list by performing integer division (//) of n by 2. This value represents the number of elements on each side of zero.

  • Create a list using the range function, starting from -half and ending at half + 1. The range will generate numbers from -half to half, inclusive. By adding 1 to half, we ensure that zero is included in the list.

  • Convert the range object to a list using the list function.

By following these steps, we ensure that the values in the list will be symmetrically distributed around zero.

Input-Output Scenarios

Let's go through some input-output scenarios:

# Input size = 5 (odd)
size = 5
half = size // 2
centered_list = list(range(-half, half + 1))
print(f"Input: {size}")
print(f"Output: {centered_list}")
Input: 5
Output: [-2, -1, 0, 1, 2]

The input size integer value is odd so the output list is created with elements ranging from -2 to 2, centered on zero.

# Input size = 6 (even, adjusted to 7)
size = 6
if size % 2 == 0:
    size += 1
half = size // 2
centered_list = list(range(-half, half + 1))
print(f"Input: 6 (adjusted to {size})")
print(f"Output: {centered_list}")
Input: 6 (adjusted to 7)
Output: [-3, -2, -1, 0, 1, 2, 3]

The input size is even, and the output list is created with elements (adjusts to the next odd number 7) ranging from -3 to 3, centered on zero.

Using the range() Function

In this approach, we utilize the range() function to generate a sequence of numbers representing the centered list. The half-size of the list is determined by dividing n by 2 using the floor division operator (//). By starting from -half and ending at half + 1 in the range, we ensure that the resulting list is centered on zero.

Example 1: Using list() Function

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, adjust it to the next odd number
        n += 1

    half = n // 2
    centered_list = list(range(-half, half + 1))
    return centered_list

# Define the size of the centered list
size = 9
centered_list = create_centered_list(size)
print('Output centered list:', centered_list)
Output centered list: [-4, -3, -2, -1, 0, 1, 2, 3, 4]

Example 2: Using List Comprehension

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, adjust it to the next odd number
        n += 1
        
    half = n // 2
    centered_list = [x for x in range(-half, half + 1)]
    return centered_list

# Define the size of the centered list
size = 7
centered_list = create_centered_list(size)
print('Output centered list:', centered_list)
Output centered list: [-3, -2, -1, 0, 1, 2, 3]

Using NumPy arange() Function

The np.arange() function in the NumPy library provides another way to create a sequence of numbers with specified start, stop, and step parameters.

Example

import numpy as np

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, adjust it to the next odd number
        n += 1
    half = n // 2
    centered_list = np.arange(-half, half + 1)
    return centered_list.tolist()

# Define the size of the centered list
size = 4
centered_list = create_centered_list(size)
print('Output centered list:', centered_list)
Output centered list: [-2, -1, 0, 1, 2]

Comparison of Methods

Method Library Required Best For
range() + list() None Simple, built-in approach
List comprehension None Pythonic, readable syntax
np.arange() NumPy Integration with NumPy arrays

Conclusion

Creating a list centered on zero is straightforward using Python's range() function or NumPy's arange(). The key is adjusting even-sized inputs to odd numbers and calculating the half-size for symmetric distribution around zero.

Updated on: 2026-03-27T13:53:57+05:30

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements