1

I have multiple lists like following-

list1=['Tom']
list2=[16]
list3=['Maths','Science','English']
list4=['A','B','C']

I want to zip these lists to achieve the following mapping-

desired results-

[('Tom', 16, 'Maths','A'), ('Tom', 16, 'Science','B'), ('Tom', 16, 'English','C')]

Result i am getting by using the following command-

results=zip(list1,list2,list3,list4)
[('Tom', 16, 'Maths','A')]

this is just an example of my problem.If a generalised solution is provided it would be helpful. If I use the statement-

res= itertools.izip_longest(*[x[c] for c in cols])

I am getting multiple rows but getting null for the name and age column. Also consider passing the column names in the above way since the names of columns are not static.

Visualisation App
  • 678
  • 3
  • 9
  • 25
  • 1
    Possible duplicate of [How to zip two differently sized lists?](https://stackoverflow.com/questions/19686533/how-to-zip-two-differently-sized-lists) – Nils Werner Jul 02 '18 at 09:41
  • 2
    assuming `list1` had 2 entries (e.g., `['Tom', 'Lisa']`), how would you do it? Who would get the `'B'` in `'Science'`? – Ma0 Jul 02 '18 at 09:41
  • 2
    This *seems* like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Are `list1` and `list2` genuine lists, i.e. can they have multiple items? And if so, how would you want to combine them? – jpp Jul 02 '18 at 09:43
  • @jpp list1 and list2 won't have mulitple items – Visualisation App Jul 02 '18 at 09:57
  • @VisualisationApp Then they should not be lists in the first place. The problem would be much easier then with a cleaner solution. – Ma0 Jul 02 '18 at 09:58
  • @NilsWerner, I don't believe this is a duplicate. OP isn't looking to zip 4 lists since the first 2 only have one item (see also my answer). – jpp Jul 02 '18 at 10:58

2 Answers2

2

You are not looking to zip 4 lists here since, as you have confirmed, the first 2 lists only ever contain one item.

Instead, use a list comprehension and zip just the final 2 lists:

name, age = list1[0], list2[0]
res = [(name, age, subject, grade) for subject, grade in zip(list3, list4)]

print(res)

[('Tom', 16, 'Maths', 'A'),
 ('Tom', 16, 'Science', 'B'),
 ('Tom', 16, 'English', 'C')]
jpp
  • 159,742
  • 34
  • 281
  • 339
  • this is just an example of my problem.if a generalised solution is provided it would be helpful.If I use the statement res= itertools.izip_longest(*[x[c] for c in cols]) i am getting multiple rows but getting null for the name and age column. Also consider passing the column names in the above way – Visualisation App Jul 02 '18 at 12:13
1

I assume here that list4 is the list the one with the most elements.

list1 = ['Tom']
list2 = [16]
list3 = ['Maths', 'Science', 'English']
list4 = ['A', 'B', 'C']
longest = (list4 if (len(list4) > len(list3)) else list3)
shortest = (list3 if (longest == list4) else list4)

for list_ in (list1, list2, shortest):
    while len(list_) < len(longest):
        list_.append(list_[-1])

zip_list = zip(list1, list2, shortest, longest)
  • why cycle `list3` but not `list4`? – Ma0 Jul 02 '18 at 09:45
  • This works for the current example, but doesn't work as I'd expect when there are more than 1 element in lists 1 or 2 (alternating between two different names for example) – Sayse Jul 02 '18 at 09:46
  • If you `cycle` all the lists you will have an infinite `zip_list`. I cycled `list3` just in case it would be shorter. –  Jul 02 '18 at 09:47
  • I will code another solution to solve this new version of the problem @Sayse. –  Jul 02 '18 at 09:48
  • It would probably better to wait until the OP has made their question clear. – Sayse Jul 02 '18 at 09:50
  • @Sayse list1 and list2 won't have mulitple items and also we don't know which of the two lists will be with most elements – Visualisation App Jul 02 '18 at 10:00