439,942 Members | 1,788 Online
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

# multiplying indeces

 P: 31 say i have L=[1,2,3] and L2=[2,3,4], how i i write a function that multiplies the same indeces together and return as a list?....the answer to the example would be [2,6,12].. -mike Feb 22 '09 #1
4 Replies

 P: 11 Just made an empty list and wrote a function to mulitply and put the answer into the list. Expand|Select|Wrap|Line Numbers L = [1,2,3] L2 = [2,3,4] multList = []   def multInde():     multList.append(L[0]*L2[0])     multList.append(L[1]*L2[1])     multList.append(L[2]*L2[2])   multInde() print multList   Feb 22 '09 #2

 100+ P: 126 A list comprehension with some zip in it, Expand|Select|Wrap|Line Numbers >>> l1 =[1,2,3] >>> l2 = [2,3,4] >>> [l[0]*l[1] for l in zip(l1,l2)] [2, 6, 12] >>>    Feb 22 '09 #3

 Expert 100+ P: 469 Interesting; I didn't know about zip. Another solution: Expand|Select|Wrap|Line Numbers [L[i] * L2[i] for i in xrange(len(L))] Feb 22 '09 #4

 Expert Mod 2.5K+ P: 2,851 My contribution, using zip(). Expand|Select|Wrap|Line Numbers >>> L1=[1,2,3] >>> L2=[2,3,4] >>> [a+b for a, b in zip(L1, L2)] [3, 5, 7] >>>  Feb 23 '09 #5