Oct-14-2019, 01:55 PM
Hi,
I found this exercise to improve my knowlegde about list, but I don't understand the delivery of the exercise:
Why in the second list are all even numbers and why instead takes only some elements?
Output:
I added my solution, which I don't know if it is correct for the exercise request.
-my code-
Regards,
RavCoder
I found this exercise to improve my knowlegde about list, but I don't understand the delivery of the exercise:
1 |
Given a two list . Create a third list by picking an odd - index element from the first list and even index elements from second. |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
listOne = [ 3 , 6 , 9 , 12 , 15 , 18 , 21 ] listTwo = [ 4 , 8 , 12 , 16 , 20 , 24 , 28 ] listThree = list () oddElements = listOne[ 1 :: 2 ] print ( "Element at odd-index positions from list one" ) print (oddElements) EvenElement = listTwo[ 0 :: 2 ] print ( "Element at odd-index positions from list two" ) print (EvenElement) print ( "Printing Final third list" ) listThree.extend(oddElements) listThree.extend(EvenElement) print (listThree) |
1 2 3 4 5 6 |
Element at odd - index positions from list one [ 6 , 12 , 18 ] Element at odd - index positions from list two [ 4 , 12 , 20 , 28 ] Printing Final third list [ 6 , 12 , 18 , 4 , 12 , 20 , 28 ] |
-my code-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
listOne = [ 3 , 6 , 9 , 12 , 15 , 18 , 21 ] listTwo = [ 4 , 8 , 12 , 16 , 20 , 24 , 28 ] listThree = [] for i in listOne: if i % 2 ! = 0 : listThree.insert( 0 ,i) print (listThree) listThree = listThree + listTwo print ( sorted (listThree)) |
RavCoder