0

I am writing a script that when you put in a buisness name, it gets the phone number and address for the place.

Unfortunately the phone numbers and adresses are in different lists, and I need to figure out how to combine the lists with a sort of a 'Go here, skip one, go here" sort of thing.

EXAMPLE:

i = ['a', 'b', 'c']
l = ['1', '2', '3']

How could I combine these so it becomes

['1', 'a', '2', 'b', '3', 'c']
Will
  • 67
  • 7

2 Answers2

0

You can use zip:

i = ['a', 'b', 'c']
l = ['1', '2', '3']

for j,k in zip(l,i):
    result.extend((j,k))

Output:

>>> result
['1', 'a', '2', 'b', '3', 'c']
Farhan.K
  • 3,425
  • 2
  • 15
  • 26
0

You can loop over the two lists and insert each of its elements in a new list following the desired order.

 a=len(i)-1  #len-1 because i used range which goes until n-1
    b=len(l)-1
    mx=max(a,b)
    new_list=[]  #new list to store the ordered elements
    for n in range(mx): #loop until it reaches the last value of the larger list
        if n<=a:
            new_list.append(l[n])
        elif n<=b:
            new_list.append(i[n])

    print new_list
João Pedro
  • 546
  • 1
  • 5
  • 19
  • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – CodeMouse92 Apr 19 '16 at 19:02
  • I appreciate your constructive critics so i edited the answer. – João Pedro Apr 20 '16 at 08:07