[Solved] python3 group items in list seperated by at least three empty items


This should work for you

lst = ['a','b',' c', '', '', '', 'd','e','f','','k','j']

res_lst = []
res_str=""
for i, item in enumerate(lst):
    res_str += item # Add current item to res_str

    # If next 3 items result is an empty string
    # and res_str has value
    if not ''.join(lst[i+1:i+3]) and res_str:
        res_lst.append(res_str)
        res_str=""

print(res_lst)

Note: Don’t use input as a variable name (unless you’re just using that to demonstrate what an example input is) as it will override the built-in input method.

solved python3 group items in list seperated by at least three empty items