How to zip unequal lists as a product of first list with others?
How to zip unequal lists as a product of first list with others?
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.
assuming
list1
had 2 entries (e.g., ['Tom', 'Lisa']
), how would you do it? Who would get the 'B'
in 'Science'
?– Ev. Kounis
Jul 2 at 9:41
list1
['Tom', 'Lisa']
'B'
'Science'
This seems like an 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 2 at 9:43
list1
list2
@jpp list1 and list2 won't have mulitple items
– Visualisation App
Jul 2 at 9:57
@VisualisationApp Then they should not be lists in the first place. The problem would be much easier then with a cleaner solution.
– Ev. Kounis
Jul 2 at 9:58
2 Answers
2
You are not looking to zip
4 lists here since, as you have confirmed, the first 2 lists only ever contain one item.
zip
Instead, use a list comprehension and zip
just the final 2 lists:
zip
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')]
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 2 at 12:13
I assume here that list4
is the list the one with the most elements.
list4
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
?– Ev. Kounis
Jul 2 at 9:45
list3
list4
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 2 at 9:46
If you
cycle
all the lists you will have an infinite zip_list
. I cycled list3
just in case it would be shorter.– Blincer
Jul 2 at 9:47
cycle
zip_list
list3
I will code another solution to solve this new version of the problem @Sayse.
– Blincer
Jul 2 at 9:48
It would probably better to wait until the OP has made their question clear.
– Sayse
Jul 2 at 9:50
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of How to zip two differently sized lists?
– Nils Werner
Jul 2 at 9:41