Create a list by picking an odd-index items from the first list and even index items from the second
Given two lists, l1 and l2, write a program to create a third list l3 by picking an odd-index element from the list l1 and even index elements from the list l2.
Given:
Expected Output:
Element at odd-index positions from list one
[6, 12, 18]
Element at even-index positions from list two
[4, 12, 20, 28]
Printing Final third list
[6, 12, 18, 4, 12, 20, 28]
Hint:
Use list slicing
Solution:
To access a range of items in a list, use the slicing operator
:
. With this operator, you can specify where to start the slicing, end, and specify the step.For example, the expression
list1[ start : stop : step]
returns the portion of the list from index start to index stop, at a step size step.