2

Appreciate your help, this is a question about list of lists.
Given lists:\

g = [[] for _ in range(5)]
a = [1,2,3,4]
b = [37100,3710,371,37,0]

I need the result to be g = [[37100,1],[3710,2],[371,3],[37,4],[0]], I tried to use nested for loops, but that is not working, below is my code:


    g = [[] for _ in range(5)]
    a=[1,2,3,4]
    b = [37100,3710,371,37,0]
    for i in range(len(b)):
      g[i].append(b[i])
    for i in g[0:5]:
      for b in range(len(a)):
        lst = []
        lst.append(a[b])
        i.append(lst)
    print(g)

However, my result is [[37100, [4]], [3710, [4]], [371, [4]], [37, [4]], [0, [4]]] Anyone knows how to solve it?

azro
  • 53,056
  • 7
  • 34
  • 70
Alicia
  • 75
  • 8
  • Your code gives me `[[37100, [1], [2], [3], [4]], [3710, [1], [2], [3], [4]], [371, [1], [2], [3], [4]], [37, [1], [2], [3], [4]], [0, [1], [2], [3], [4]]]` haha – azro May 12 '21 at 08:14
  • This would help https://stackoverflow.com/questions/19686533/how-to-zip-two-differently-sized-lists – N. Arunoprayoch May 12 '21 at 08:15

5 Answers5

2

You can use itertools.zip_longest

from itertools import zip_longest
a = [1,2,3,4]
b = [37100,3710,371,37,0]

c = [1,2,3,4,5,6]
d = [1,2,3,4,5,6,7]

res = [[ii for ii in i if ii is not None] for i in zip_longest(b, a, c)]
print(res)

res = [[ii for ii in i if ii is not None] for i in zip_longest(b, a, c, d)]
print(res)

Output

[[37100, 1, 1], [3710, 2, 2], [371, 3, 3], [37, 4, 4], [0, 5], [6]]
[[37100, 1, 1, 1], [3710, 2, 2, 2], [371, 3, 3, 3], [37, 4, 4, 4], [0, 5, 5], [6, 6], [7]]

Edit based on comment:

from itertools import repeat
a = [1,2,3,4]
b = [371] * 100
c = [11,12,13,14]
d = [111,222,333,444]
res = [list(j) for i in zip(repeat(b), (a, c, d)) for j in zip(*i)]

print(res)
python_user
  • 5,375
  • 2
  • 13
  • 32
  • Thank you for your response, is it only available for two lists inputs? What if I also need to add more elements in each list of lists? for example, c = [1,2,3,4,5,6], and result is [[37100, 1,1], [3710, 2,2], [371, 3,3], [37, 4,4], [0,5],[6]] followed by d = [1,2,3,4,5,6,7], and final result is [[37100, 1,1,1], [3710, 2,2,2], [371, 3,3,3], [37, 4,4,4], [0,5,5],[6,6][7]]? – Alicia May 12 '21 at 08:28
  • no @Alicia, you can use them with any number of lists, check the new edit, you need to just change the parameters to `zip_longest` in the order in which you want to pair, like I have shown – python_user May 12 '21 at 08:30
  • 1
    Appreciate your help and your patient, I will try in my code now – Alicia May 12 '21 at 08:35
  • Sorry I am back again, for the code above, if b is a list of 371*N and a remains unchanged, but c=[11,12,13,14], d = [111,222,333,444] and I need the result to be [[371, 1], [371, 2,], [371, 3,], [371, 4,], [371, 11],[371,12][371, 13], [371,14],[371,111],[371,222],[371,333],[371,444].....] as I just realised the new added list will not always be added in g[0] position, sorry for the trouble and would you mind helping this as well? – Alicia May 12 '21 at 08:57
  • sure, but I dont quite understand, what do you mean by "newly added list", you have 4 lists `a`, `b`, `c` and `d` in which `a` is the same and others take the new values as you have commented, what are the lists that you want to pair now? @Alicia – python_user May 12 '21 at 09:01
  • so you want to pair `b` with `a` then `b` with `c` then `b` with `d`? the example you have shown now has pairs as opposed to what you have originally asked – python_user May 12 '21 at 09:11
  • Thank you and sorry to not making it clear, for the zip_longest, if we just have a and b list, our pair is g[[a[0],b[0]],[[a[1],[b[1]],...], but now I want my g list to be g[[b[0]],[[b[0]],[a[0],b[0]],[a[1],b[0]], the b list is actually exist in each element of list g, which means the new added elements should be paired in any position of list g, and the "inserted pair length" was decided by the length of list a, I am quite new to python, hope I made myself clear, thank you – Alicia May 12 '21 at 09:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232274/discussion-between-python-user-and-alicia). – python_user May 12 '21 at 09:13
1

Here a solution that isn't data-dependant

a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = [[] for _ in range(max(len(a), len(b)))]
for i, sublist in enumerate(g):
    sublist.extend(b[i:i + 1])
    sublist.extend(a[i:i + 1])

Or with g unprepared

a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = []
for i in range(max(len(a), len(b))):
    g.append(b[i:i + 1] + a[i:i + 1])

Or itertools.zip_longest allows to pair data, and fill with a default value when one iterable is shorter, then you have to filter out that default value (None by default)

a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = [[val for val in pair if val is not None]
     for pair in zip_longest(b, a)]
azro
  • 53,056
  • 7
  • 34
  • 70
0
# Assuming len(a) <= len(b)
g = []
for i in range(len(a)) :
  g.append([b[i], a[i]])
for i in range(len(a), len(b)) :
  g.append([b[i]])
RandomGuy
  • 1,076
  • 6
  • 17
  • data-dependant solution aren't best as they are not the easiest to reproduce – azro May 12 '21 at 08:20
  • Thank you so much for your help, if I am not sure about the length of input lists, can this method still be available, just what I replied to @python_user, what if I need to add more lists as input? and list g has no size at the beginning, I will only know the length of g after all input lists are involved – Alicia May 12 '21 at 08:31
0

Using map and lambda

a = [1,2,3,4]
b = [37100,3710,371,37,0]
map(lambda x, y: [x, y] if x is not None else [y], a, b)

Output

[[37100, 1], [3710, 2], [371, 3], [37, 4], [0]]

In the case of lengh a and b not equal

map(lambda x, y: [x, y] if x is not None and y is not None else ([y] if y is not None else [x]), a, b)
khelili miliana
  • 3,730
  • 2
  • 15
  • 28
-1

the expected output can be obtained by:

[list(x) for x in zip(a,b)] + [b[len(a):]]

It clearly depends on your data, and the general purpose of your code.

to read more about zip, see the official docs: https://docs.python.org/3/library/functions.html#zip

Ammar
  • 1,305
  • 2
  • 11
  • 16