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 :

By cbsepython

A complete solution for the students of class 9 to 12 having subject Information Technology (402), Computer Science (083). Explore our website for all the useful content as Topic wise notes, Solved QNA, MCQs, Projects and Quiz related to the latest syllabus.

Leave a Reply

Your email address will not be published. Required fields are marked *