0

I want to combine multiple lists into 1 list without using zip() since zip() will convert expected_list to a list of tuples. I want expected_result is a list of lists.

list1 = [ "a" 
          "b" 
          "c" ]
list2 = [ "e" 
          "f"
          "g" ]

expected_list = [ [ "a", "e" ]
                  [ "b" ,"f" ]  
                  [ "c" ,"g" ] ]

any solution for this ?

Chris
  • 112,704
  • 77
  • 249
  • 231
mht
  • 123
  • 1
  • 8
  • thanks Chirs for edding my post. How could you put zip() into the grey background ? – mht Dec 11 '20 at 16:40
  • 1
    have you tried using list comprehensions in conjunction with zip? – jtitusj Dec 11 '20 at 16:41
  • Why not use `zip`, but then convert the `tuple`s to `list`s? `expected_list = [list(t) for t in zip(list1, list2)]` – 0x5453 Dec 11 '20 at 16:41
  • 1
    Note that `list1` and `list2` here each have one member: a three character string. You're missing commas so Python is concatenating adjacent string literals. – Chris Dec 11 '20 at 16:41
  • @mht, inline code can be marked with backticks (`\``), e.g. `...without using \`zip()\` since...`. – Chris Dec 11 '20 at 16:43

1 Answers1

2

Try this:

[[i, j] for i, j in zip(list1, list2)]

Or as ekhumoro wrote below:

list(map(lambda *x: list(x), a, b)).
ncopiy
  • 1,275
  • 11
  • 28
  • The question specifically says `without` using zip. Your answer uses zip. – Chris Doyle Dec 11 '20 at 16:44
  • 4
    @ChrisDoyle, but this was an [XY problem](https://meta.stackexchange.com/q/66377/248627). OP _thought_ the solution was not to use `zip()`, but what they really wanted was a list of lists. This gives that result. – Chris Dec 11 '20 at 16:47
  • 1
    FWIW - without zip: `list(map(lambda *x: list(x), a, b))`. – ekhumoro Dec 11 '20 at 17:04