[Python] wap to transfer content from 1 list to 2 different list on the basis of odd and even indices. Elements at odd places in one list and the elements on even index in the other
Answers
Heya friend,
Here we go:
Before we move on to the code, let's understand this program first.
- Here we will take two empty lists odd and even in the beginning.
- We'll calculate the length of the primary list and work for that number of times.
- We will append the elements at the even index number to the list even and the element at the odd index number to the odd list using append function.
- If the index number is divisible by two, we can say it is even, else it is ought to be ought.
NOTE :- Keep in mind that the index number start from 0 .
Now let's move on to the code:
SOURCE CODE
#to transfer content of a list to two different list based on their index numbers
a = eval(input('Enter any list:'))
odd=[]
even=[]
l=len(a)
for i in range (l):
if i%2==0:
even.append(a[i])
else:
odd.append(a[i])
print()
print('List containing elements from odd index position :',odd)
print()
print('List containing elements from even index position :',even)
OUTPUT
Enter any list: [10,20,30,40,50,60]
List containing elements from odd index position : [20, 40, 60]
List containing elements from even index position : [10, 30, 50]
>>>
Enter any list: [3,6,5,9,2,7]
List containing elements from odd index position : [6, 9, 7]
List containing elements from even index position : [3, 5, 2]
>>>