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

Last updated on February 8th, 2022 at 11:23 am

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 :

Jitendra Singh
✔ Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me →

Leave a Comment