1

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...]

szotyi
  • 13
  • 2

8 Answers8

2

The shortest solution may be:

list(sum(zip(t2, t1), ()))
Squall
  • 594
  • 4
  • 15
  • 2
    In general, [don't use `sum` to flatten a list, it is O(n^2)](http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python/952952#952952). – kennytm May 20 '16 at 11:56
2
  1. In Python you don't need to "reserve capacity" for a list. Just write

    t3 = []
    

    In fact, t3 = [len(t1)+len(t2)] doesn't even creates a list with length 24, but creates a list with a single entry [24].

  2. t1[a] and t2[a] are elements you want to add to the list. To add an element, you use the .append method:

    t3.append(t1[a])
    t3.append(t2[a])
    

    .extend is used to add a list (in fact, any iterable) to a list, e.g.

    t3.extend([t1[a], t2[a]])
    
  3. The problem itself can be solved easily using list comprehensions.

    [a for l in zip(t2, t1) for a in l]
    

There are many other improvements could be made (e.g. use a for loop instead of a while loop). You could take it to http://codereview.stackexchange.com.


(BTW, this code does not handle leap year.)

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

Just zip the lists and flatten the result.

>>> from itertools import chain
>>> 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']
>>> list(chain(*zip(t2, t1)))
['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]

Without chain:

>>> [x for tup in zip(t2, t1) for x in tup]
['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]
timgeb
  • 76,762
  • 20
  • 123
  • 145
1

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)
David Guyon
  • 2,759
  • 1
  • 28
  • 40
0

you probably have to read more about python lists and their methods. t3= [len(t1)+len(t2)] this is not necessary at all. I guess you have a C background and trying to initialize the list with size. In python you don't have to initialize the list size (its auto increasing). And the items you have in a list are not stored as per the sequence you have entered them in. Please check tuple in python if you want your items to be in the same sequence. Happy Coding

manoj prashant k
  • 339
  • 1
  • 13
0
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']

arr = []

for i in range(12):
    arr.append(t2[i])
    arr.append(t1[i])

print(arr)

Output -

['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]

You can alternatively write -

import itertools

arr = list(itertools.chain.from_iterable(zip(t2, t1))
Vedang Mehta
  • 2,214
  • 9
  • 22
0

In Python, you can't create lists with a fixed length like you can do with arrays in other languages, so the third line should just be t3 = [].

Also, the extend() function is used to concatenate lists. To add a single new value, you need to use the append() function instead.

tjohnson
  • 1,047
  • 1
  • 11
  • 18
0

Python is dynamic programming language, the type of the identifier is determined when it is assigned value. so basically you can do in this way:

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= []
for a in range(len(t1)):
    append.append(t1[a])
    apppend.append(t2[a])
print t3
Vincent
  • 26
  • 4