Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

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 11 hours ago 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 11 hours ago 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 11 hours ago by avibootz
...