Input a list of numbers and swap elements at the even location with the elements at the odd location Python Program

 

# Program to input number list and swapping odd and even index elements 

 

# Entering 5 element Lsit

mylist = []

print("Enter 5 elements for the list: ")

for i in range(5):

    value = int(input())

    mylist.append(value)

# printing original list

print("The original list : " + str(mylist))

# Separating odd and even index elements

odd_i = []

even_i = []

for i in range(0, len(mylist)):

    if i % 2:

        even_i.append(mylist[i])

    else :

        odd_i.append(mylist[i])

result = odd_i + even_i

# print result

print("Separated odd and even index list: " + str(result))

 

Output:

Enter 5 elements for the list: 
4
7
16
7
9
The original list : [4, 7, 16, 7, 9]
Separated odd and even index list: [4, 16, 9, 7, 7]
>>> 

 

You may also Check :

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!