Create a List Centered on Zero in Python



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 the 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 integer = 5
Output centered list [-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 integer = 6
Output centered list [-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. To convert the range object into a list, we apply the list() function.

Example

In this example, we will define a function that takes an integer as input and creates a list centered on zero based on that value.

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, we 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

Output centered list: [-4, -3, -2, -1, 0, 1, 2, 3, 4]

Example

This example works similarly to the previous one but instead of using the list() function here, we will use the list comprehension to convert the range object into a list.

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, we 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 = 15
centered_list = create_centered_list(size)
print('Output centered list:',centered_list)

Output

Output centered list: [-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]

Using the np.arange() function

Here, the np.arange() function in the NumPy library is used to create a sequence of numbers with a specified start, stop, and step size.

Example

In this example, the numpy library is used to create a centered list.

import numpy as np

def create_centered_list(n):
    if n % 2 == 0:
        # If n is even, we 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

Output centered list: [-2, -1, 0, 1, 2]
Updated on: 2023-08-29T14:29:15+05:30

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements