szotyi szotyi - 6 months ago 12
Python Question

python appending elements to a list from a list

I would like to create a list that adds elements alternately from 2 seperate lists in python .
I had the following idea but it doesn't seem to work:

t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3= [len(t1)+len(t2)]
a = 0

while a < len(t1)+len(t2):
t3.extend(t1[a])
t3.extend(t2[a])
a = a + 1
print t3


So basically I would like ['Jan',31,'Feb',28,'Mar',31, ect...]

Answer

Here you go:

t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
      'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3 = list()

for i, j in zip(t1, t2):
  t3.append(i)
  t3.append(j)

print(t3)