Python Forum
Understand task in this exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understand task in this exercise
#1
Hi,
I found this exercise to improve my knowlegde about list, but I don't understand the delivery of the exercise:
Given a two list. Create a third list by picking an odd-index element from the first list and even index elements from second.
Why in the second list are all even numbers and why instead takes only some elements? Huh
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)
Output:
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]
I added my solution, which I don't know if it is correct for the exercise request.
-my code-
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))
Regards,
RavCoder
Reply
#2
The odd and even is the index, not the item. So from listOne it is taking the odd indexes: listOne[1], listOne[3], and listOne[5]. It just so happens that those are all even numbers: 6, 12, and 8. Then it takes the even indexes from listTwo: listTwo[0], listTwo[2], listTwo[4], and listTwo[6].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
So I was wrong to execute my code, because I understood to take the elements not the indexes
Reply
#4
That's my reading of it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  count certain task in task manager[solved] kucingkembar 2 1,122 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  Schedule a task and render/ use the result of the task in any given time klllmmm 2 2,099 May-04-2021, 10:17 AM
Last Post: klllmmm
  How to create a task/import a task(task scheduler) using python Tyrel 7 3,727 Feb-11-2021, 11:45 AM
Last Post: Tyrel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020