-1

we have two lists, one for months and the other for numbers of days, the last one is disordered, for exemple :

t1 = [28,31,31,30,31,30,31,31,31,30,30,31]
t2 = ['Jan','Feb','March','Avr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

and we want to associate them in order to get the following result : ['Jan', 31, 'Fev', 28, 'Mars', 31, 'Avr', 30, 'Mai', 31, 'Juin', 30, 'Juil', 31, 'Aout', 31, 'Sep', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31] by using python, the first list should be ordered before using extend() function to associate the two lists, so that each month can be followed by its number of days but I'm wondering if there is any other way to do that without putting the first list in order?

  • 2
    What do you mean by "the last one is disordered" - the first one seems to be in the wrong order, if any? How do you expect them to be matched other than the script already knowing it needs to match "number of days" in month with the name of the month? What have you tried yourself? – Grismar Nov 14 '21 at 00:25
  • Another way to look at your problem: how would you match pairs in these two lists? Can you do it without using the knowledge you have about how many days each month has? And if not, how do you expect a script to perform that task? – Grismar Nov 14 '21 at 00:27
  • Why do you have the number of days in a month in an unordered list? Why not just generate the correct number of days for each month instead of ordering this list? – Iain Shelvington Nov 14 '21 at 00:27

2 Answers2

1
t1 = [28,31,31,30,31,30,31,31,31,30,30,31]
t2 = ['Jan','Feb','March','Avr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
# this is main list   
mainList = [[t2,t1][x%2][x//2] for x in range(24)]
  • 1
    Please, try to describe the rational behind your answer instead of a piece od code. Please also note that your answer doesn't solve the requester's question. – Michael Doubez Nov 14 '21 at 21:37
0
finall_list = []
for i in range(len(t2)):
    finall_list.append(t2[i])
    finall_list.append(t1[i])
print(finall_list)
  • Welcome to Stack Overflow, and thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Nov 15 '21 at 00:45