Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
How to make a list comprehension in python with unequal sublists

I have a list of unequal lists. I would like to generate a new list with list comprehension from the sublists.

s = [['a','b','c','d'],['e','f','g'],['h','i'],['j','k','l','m']]

I am trying the following code but it keeps raising an indexError:

new_s = []
for i in range(len(s)):
    new_s.append((i,[t[i] for t in s if t[i]))

The expected output would be:

new_s = [(0,['a','e','h','j']),(1,['b','f','i','k']),(2,['c','g','l']),(3,['d','m'])]

Any ideas how to get this to work?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use itertools.zip_longest to iterate over each sublist elementwise, while using None as the fill value for the shorter sublists.

Then use filter to remove the None values that were used from padding.

So all together in a list comprehension:

>>> from itertools import zip_longest
>>> [(i, list(filter(None, j))) for i, j in enumerate(zip_longest(*s))]
[(0, ['a', 'e', 'h', 'j']), (1, ['b', 'f', 'i', 'k']), (2, ['c', 'g', 'l']), (3, ['d', 'm'])]
over 4 years ago · Santiago Trujillo Report

0

one without itertools but modifying the original list.

def until_depleted():
   while any(sl for sl in s):
    yield 1

list(enumerate(list(s_l.pop(0) for s_l in s if s_l) for _ in until_depleted()))

one without modifying the original but with a counter

idx = 0
max_idx = max(len(_) for _ in s)

def until_maxidx():
    global idx
    while idx < max_idx:
        yield 1
        idx += 1

list(enumerate(list(s_l[idx] for s_l in s if idx < len(s_l)) for _ in until_maxidx()))

A more explicit one without the inner comprehension nor calling generators:

ret = []
idx = 0
max_idx = max(len(_) for _ in s)
while idx < max_idx:
    ret.append(list(s_l[idx] for s_l in s if idx < len(s_l)))
    idx += 1

print(list(enumerate(ret)))
over 4 years ago · Santiago Trujillo Report

0

This is without itertools, but also without a comprehension, so I don't know if it does count as a solution.

s = [['a','b','c','d'],['e','f','g'],['h','i'],'j','k','l','m']]

new_s = []
for i in range(len(s)):
    tmp = []
    for item in s:
        tmp.extend(item[i:i+1])
    new_s.append((i, tmp))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!