How to create a list with zero in the center in Python

3 Answers

0 votes
def create_list_center_zero(n):
    lst = [1] * n
    lst[n // 2] = 0 
    
    return lst

print(create_list_center_zero(7))


'''
run:

[1, 1, 1, 0, 1, 1, 1]

'''

 



answered Feb 20 by avibootz
0 votes
def create_list_center_zero(n):
    half = n // 2
    lst = list(range(half, 0, -1)) + [0] + list(range(1, half + 1))
    
    return lst

print(create_list_center_zero(7))


'''
run:

[3, 2, 1, 0, 1, 2, 3]

'''

 



answered Feb 20 by avibootz
0 votes
def create_list_center_zero(n):
    if n % 2 == 0:
        # If n is even, make it odd to add zero inthe  center
        n += 1

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

print(create_list_center_zero(10))


'''
run:

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

'''

 



answered Feb 20 by avibootz
...