0

I'm working a on a list like this, a = ['a','b','','','c','d'], the real list is including thousands of data entries. Is there a fancy way to make the list a as [['a','b'],['c','d]] because the data is really huge?

4
  • 1
    What should the output look like if there was a single (or an odd number) of blank strings in a row? Commented Nov 11, 2014 at 22:20
  • if there is a single blank string like a = ['a','','b','','','c','d'], I want [['a','','b'],['c','d]] Thank you so much Commented Nov 11, 2014 at 22:35
  • 1
    Now I have to ask about what to do for 3 (or more) blank strings in a row, since your single blank example doesn't really explain enough. Commented Nov 11, 2014 at 23:05
  • What should:['a','b','','c','d'] look like? What about: ['a','','','',b','','c','d']? Maybe you can add examples, and explain better exactly what to do with blanks. Commented Nov 11, 2014 at 23:23

1 Answer 1

4

You can use itertools.groupby for this. You basically group by consecutive empty strings, or consecutive non-empty strings. Then keep all groups that were grouped by True from the lambda in a list comprehension.

>>> from itertools import groupby

>>> [list(i[1]) for i in groupby(a, lambda i: i != '') if i[0]]
[['a', 'b'], ['c', 'd']]

For another example

>>> b = ['a','b','','','c','d', '', 'e', 'f', 'g', '', '', 'h']
>>> [list(i[1]) for i in groupby(b, lambda i: i != '') if i[0]]
[['a', 'b'], ['c', 'd'], ['e', 'f', 'g'], ['h']]
Sign up to request clarification or add additional context in comments.

3 Comments

In this case, you could use bool instead of lambda i: i != '', in the same way you'd normally write if i: instead of if i != '':. I'm not sure whether that would be more readable or less in this case—it makes it less explicit that you're dealing with strings, but it also cuts a lot of irrelevant boilerplate out of the expression, so… I guess just try it both ways and see which seems more readable to you.
Also, I think it might be more readable as [list(g) for k, g in groupby(…)], but that's really nitpicky, so feel free to ignore it.
Thank you guys so much! But I don't want it separate when there is only one blank string. like a = ['a','','b','','','c','d'], what I expect is [['a','','b'],['c','d]].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.