Harvey Dent
Harvey Dent

Reputation: 339

Python merge items from two list

I have the following codes:

    for j in range(4):
      print md_array[j:j+1]
    for j in range(4):  
      print y_array[j:j+1]
    for j in range(4):
      print md_array[j:j+1]+y_array[j:j+1]
    for j in range(4):
      print "%s%s" % (md_array[j:j+1], y_array[j:j+1])

The results are:

['12/1/']['12/2/']['12/3/']['12/34/']
['2011']['2010']['2009']['2008']
['12/1/', '2011']['12/2/', '2010']['12/3/', '2009']['12/34/', '2008']
['12/1/']['2011']['12/2/']['2010']['12/3/']['2009']['12/34/']['2008']

BUT actually I want to output

    ['12/1/2011']['12/2/2010']['12/3/2009']['12/34/2008']

I tried append but failed...So what's wrong with my codes??

Upvotes: 0

Views: 316

Answers (2)

John La Rooy
John La Rooy

Reputation: 304137

print "".join("[%s%s]"%t for t in zip(md_array, y_array))

Upvotes: 1

muhuk
muhuk

Reputation: 16085

array[start_index:end_index] gives you a slice, a sub-list of the list it's applied on. In this case, you want to get a single item. Try the following code:

for j in range(4):
    print "%s%s" % (md_array[j], y_array[j])

You can read more about slicing and indexes on documentation for lists.

Upvotes: 1

Related Questions