Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to split a list to list of lists based on empty strings in Python

2 Answers

0 votes
from itertools import groupby 

lst = ['java', '', 'python', 'php', '', 'c', 'c++', 'c#', '', 'javascript'] 

lst = [list(sub) for e, sub in groupby(lst, key = bool) if e] 
  
print(lst) 
     


        
'''
run:
 
[['java'], ['python', 'php'], ['c', 'c++', 'c#'], ['javascript']]
        
'''

 



answered May 2, 2020 by avibootz
0 votes
lst = ['java', '', 'python', 'php', '', 'c', 'c++', 'c#', '', 'javascript'] 

result = [[]]
for s in lst:
    if not s:
        result.append([])
    else:
        result[-1].append(s)

print(result)
    


        
'''
run:
 
[['java'], ['python', 'php'], ['c', 'c++', 'c#'], ['javascript']]
        
'''

 



answered May 2, 2020 by avibootz
...