0

I have 1 list of names:

[VendorName1, VendorName2, VendorName3, VendorName4]

and I have a multidimensional list of emails varying in sizes:

[[[email protected]],[[email protected],[email protected]],[[email protected]],[[email protected],[email protected],[email protected]]

This is what I have tried so far but I keep getting errors.

final=[]
  count=1
  for sub1 in vendor_names:
    for sub2 in vendor_emails:
      vendor_names[count].append(final)
      vendor_emails[count].append(final[1])
  print(final)

I would like to combine the 2 lists so that the final list is like:

[[[Vendor1,[[email protected]],[Vendor2,[[email protected],[email protected]]]...]
rmcknst2
  • 195
  • 1
  • 2
  • 11
  • 1
    Ok, and what was your attempt? You don't actually have `Vendor1` in your other list, you have `VendorName1` so it seems like a half-hearted request rather than something we can achieve. Should we be splitting the vendor name? – roganjosh Jun 10 '19 at 22:16
  • @roganjosh yeah let me edit my question real quick – rmcknst2 Jun 10 '19 at 22:18
  • 3
    I think you just want [`zip()`](https://docs.python.org/3/library/functions.html#zip) – Mark Jun 10 '19 at 22:19
  • @MarkMeyer zip() looks interesting let me give it a try – rmcknst2 Jun 10 '19 at 22:20
  • Possible duplicate of [How to merge lists into a list of tuples?](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples) –  Jun 10 '19 at 22:29

2 Answers2

1

You can use zip(), but it merges lists to list of tuples.

OR

For merging lists to list of lists:

x = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
y = [['[email protected]'], ['[email protected]', '[email protected]'], [
    '[email protected]'], ['[email protected]', '[email protected]', '[email protected]']]
z = []
for i in range(len(x)):
    z.append([])
    z[i].append(x[i])
    z[i].append(y[i])
print(z)

Output:

[['VendorName1', ['[email protected]']], ['VendorName2', ['[email protected]', '[email protected]']], ['VendorName3', ['[email protected]']], ['VendorName4', ['[email protected]', '[email protected]', '[email protected]']]]
Agawane
  • 173
  • 1
  • 8
1

Zip will do this just fine:

l1 = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
l2 = [['[email protected]'],['[email protected]','[email protected]'],['[email protected]'],['[email protected],[email protected]','[email protected]']]

print(list(zip(l1, l2)))

Output:

[('VendorName1', ['[email protected]']), ('VendorName2', ['[email protected]', '[email protected]']), ('VendorName3', ['[email protected]']), ('VendorName4', ['[email protected],[email protected]', '[email protected]'])]

If you really don't want tuples (since they are immutable) you can do list comprehension but this requires that you don't have more vendors than emails:

l1 = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
l2 = [['[email protected]'],['[email protected]','[email protected]'],['[email protected]'],['[email protected],[email protected]','[email protected]']]

l3 = [[l1[x]] + [l2[x]] for x in range(len(l1))]
print(l3)

Output:

[['VendorName1', ['[email protected]']], ['VendorName2', ['[email protected]', '[email protected]']], ['VendorName3', ['[email protected]']], ['VendorName4', ['[email protected],[email protected]', '[email protected]']]]