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 :
-
Find the largest/smallest number in a list/tuple
-
Input a list/tuple of elements, search for a given element in the list/tuple.
-
Input a list of numbers and find the smallest and largest number from the list.
-
Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have scored marks above 75.