I would like to reshape the following list :
wide_list = [[1,['a','b','c']],[2,['d','e']],[3,'f']]
in a “long format”:
long_list = [[1,'a'],[1,'b'],[1,'c'],[2,'d'],[2,'e'],[3,'f']]
How can this be achieved efficiently in Python?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try a nested list comprehension:
Note, the last group had to be changed to match the pattern of the first two groups. Instead of
[3, 'f'], use[3, ['f']]instead. Otherwise, you’ll need special case logic for groups that don’t follow the pattern.