-1

I have two lists shaped like the following:

listA
[[778, 606],
 [2115, 2049],
 [3361, 3183],
 [4512, 4179]]

listB
[[-128, -4],
 [-38, 38],
 [-15, 110],
 [-105, 185]]

I want to take the first element from listA and listB, to create a list that will go into another list.

So I want an output like the following:

 new_list
 [[[778, 606],[-128, -4]], 
 [[2115, 2049],[-38, 38]], 
 [[3361, 3183],[-15, 110]], 
 [[4512, 4179],[-105, 185]]]

Thoughts on how to properly structure the list comprehension or maybe theres a better out of the box method?


I think I'm struggling with the logic of how to append lists.

I've tried the following:

x = []
for i,j in [(i,j) for i in listA for j in listB]:
x.append(i)
x.append(j)

which resulted in:

[[778, 606],
 [-128, -4],
 [778, 606],
 [-38, 38],
 [778, 606],
 [-15, 110],
 [778, 606],
 [-105, 185]]

Which isn't what I wanted. So I also tried:

y = [(i,j) for i in listA for j in listB]

which resulted in:

    [([778, 606], [-128, -4]),
     ([778, 606], [-38, 38]),
     ([778, 606], [-15, 110]),
     ([778, 606], [-105, 185]),
     ([2115, 2049], [-128, -4]),
     ([2115, 2049], [-38, 38]),
     ([2115, 2049], [-15, 110]),
     ([2115, 2049], [-105, 185]),
     ([3361, 3183], [-128, -4]),
     ([3361, 3183], [-38, 38]),
     ([3361, 3183], [-15, 110]),
     ([3361, 3183], [-105, 185]),
     ([4512, 4179], [-128, -4]),
     ([4512, 4179], [-38, 38]),
     ([4512, 4179], [-15, 110]),
     ([4512, 4179], [-105, 185])]
OfSorts
  • 196
  • 1
  • 1
  • 15
  • It sounds like you're just looking for [`zip`](https://docs.python.org/3/library/functions.html#zip). If so, the linked duplicate will explain how to do what you want. If it doesn't, please explain why it doesn't solve your problem and we can reopen. – abarnert Jul 16 '18 at 19:53
  • (almost) a duplicate of https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples – joel Jul 16 '18 at 19:54
  • If you really wanted to do it manually, you can use indices in a comprehension if you know you're dealing with lists: `y = [[listA[i], listB[i]] for i in range(len(listA))]`. If you want it to be fully general and work on any iterables, the `zip` docs show an equivalent generator you can modify to fit your purposes. But really, you just want to use `zip`. – abarnert Jul 16 '18 at 19:56
  • @JoelBerkeley thanks for the link! I've been reading through stackoverflow posts trying to find something similar. I don't think I used the correct terminology when trying to find an answer. – OfSorts Jul 16 '18 at 20:01
  • @marytay yeah sometimes we need to be told the answer before we can search for it effectively i guess – joel Jul 16 '18 at 20:02

1 Answers1

1

I'd use zip for this. It's a really nice way of looping through two lists at once and getting both of their values.

new_list = [list(c) for c in zip(listA, listB)]
new_list = [*zip(listA, listB)] #if tuples are fine.

Output:

[[[778, 606], [-128, -4]], 
[[2115, 2049], [-38, 38]], 
[[3361, 3183], [-15, 110]], 
[[4512, 4179], [-105, 185]]]
Neil
  • 14,063
  • 3
  • 30
  • 51
  • Why use a list display with a splat inside of it inside of just calling `list`? It has the same effect, but it's less readable, slower, and not backward compatible to older versions of Python, and I can't think of any advantages to counter all of that. – abarnert Jul 16 '18 at 19:57
  • @abarnert Didn't know it was slower, updated. – Neil Jul 16 '18 at 19:58
  • @neil thanks for the quick response. Using your first suggestion resulted in an error message "TypeError: 'list' object is not callable". I think this is due to the ()'s around list. Changing the ()'s to []'s results in the following error message "TypeError: list indices must be integers or slices, not tuple". In the end, I used the 2nd suggestion which worked like a charm! I definitely have to start thinking about zip more often. – OfSorts Jul 16 '18 at 20:13
  • Nvm, that error was my fault. I accidentally named a list 'list' earlier in my script which reassigned the predefined variable. – OfSorts Jul 16 '18 at 21:25