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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to reverse one dimensional int array in Python

3 Answers

0 votes
def reverse_one_dimensional_array(arr):
    arr_size = len(arr)
 
    for i in range(int(arr_size / 2)):
        tmp = arr[i]
        arr[i] = arr[arr_size - 1 - i]
        arr[arr_size - 1 - i] = tmp

    return arr
    
arr = [1, 2, 3, 4, 5, 6, 7]

reverse_one_dimensional_array(arr)

arr_size = len(arr)
for i in range(arr_size):
    print("arr[{}] = {}".format(i, arr[i]))
 
 
 
'''
run:
 
arr[0] = 7
arr[1] = 6
arr[2] = 5
arr[3] = 4
arr[4] = 3
arr[5] = 2
arr[6] = 1
 
'''

 



answered Feb 9, 2016 by avibootz
edited Mar 28, 2024 by avibootz
0 votes
arr = [1, 2, 3, 4, 5, 6, 7]

arr.reverse()

print(arr)  
 
 
 
'''
run:
 
[7, 6, 5, 4, 3, 2, 1]
 
'''

 



answered Mar 28, 2024 by avibootz
0 votes
arr = [1, 2, 3, 4, 5, 6, 7]
 
arr = arr[::-1]

print(arr)  
  
  
  
'''
run:
  
[7, 6, 5, 4, 3, 2, 1]
  
'''

 



answered Mar 29, 2024 by avibootz

Related questions

1 answer 196 views
1 answer 140 views
1 answer 187 views
2 answers 206 views
1 answer 193 views
3 answers 279 views
2 answers 284 views
...