12

I have a string, which I need to split into 2-letter pieces. For example, 'ABCDXY' should become ['AB', 'CD', 'XY']. The behavior in the case of odd number of characters may be entirely arbitrary (I'll check the length in advance).

Is there any way to do this without an ugly loop?

3
  • 2
    Hey mate... what do you mean by "ugly loop"? XD
    – Littm
    Sep 19, 2012 at 9:08
  • 1
    I didn't think about it. But now that I did... An ugly loop is a loop that is uglier than necessary, or that is present when no loop is really required :)
    – max
    Sep 19, 2012 at 9:10

6 Answers 6

26
>>> [s[i:i + 2] for i in range(0, len(s), 2)]
['AB', 'CD', 'XY']
6
  • 2
    thats a loop. OP said "no loops"
    – Inbar Rose
    Sep 19, 2012 at 9:06
  • 5
    He did say "without an ugly loop"... and this certainly avoids that. :-) Sep 19, 2012 at 9:08
  • Maybe that's the only way. I guess sometimes loops are required.
    – max
    Sep 19, 2012 at 9:09
  • Well, I understand it in the way "not a standard for loop", I was supposing list comprehensions would be good. Let's see what the OP says... :-)
    – Emmanuel
    Sep 19, 2012 at 9:10
  • 1
    List comprehensions are almost not a loop, so this would qualify :)
    – max
    Sep 19, 2012 at 9:12
16

Using regular expressions!

>>> import re
>>> s = "ABCDXYv"
>>> re.findall(r'.{1,2}',s,re.DOTALL)
['AB', 'CD', 'XY', 'v']

I know it has been a while, but I came back to this and was curious about which method was better; mine: r'.{1,2}' or Jon's r'..?'. On the surface, Jon's looks much nicer, and I thought it would be much faster than mine, but I was surprised to find otherwise, so I thought I would share:

>>> import timeit
>>> timeit.Timer("re.findall(r'.{1,2}', 'ABCDXYv')", setup='import re').repeat()
[1.9064299485802252, 1.8369554649334674, 1.8548105833383772]
>>> timeit.Timer("re.findall(r'..?', 'ABCDXYv')", setup='import re').repeat()
[1.9142223469651611, 1.8670038395145383, 1.85781945659771]

Which shows that indeed r'.{1,2}' is the better/faster choice. (But only slightly)

7
  • Careful, this fails to work correctly if the string contains newlines. Sep 19, 2012 at 9:17
  • @TimPietzcker you are right, i will add that with the flag to handle such a case.
    – Inbar Rose
    Sep 19, 2012 at 9:18
  • () are unnecessary in the regex: re.findall('(?s)..', s)
    – jfs
    Sep 19, 2012 at 9:40
  • 1
    or take the last character in the string and add that to your list at the end. -- def grouper() seems a bit convoluted - the regex can just be .{1,2} Sep 19, 2012 at 9:45
  • You could use ..? as the regex. Sep 19, 2012 at 10:31
2

You could try:

s = 'ABCDEFG'
r = [s[i:i+2] for i in xrange(0, len(s), 2)]

# r is ['AB', 'CD', 'EF', 'G']

UPDATE 2

If you don't care about odd chars, you could use a regex (avoiding the loop):

s = 'ABCDEFG'
r = re.compile('(..)').findall(s)
# r is ['AB', 'CD', 'EF']
1

There's nothing ugly about the perfectly Pythonic:

string = 'ABCDXY'
[string[i:i+2] for i in xrange(0, len(string), 2)]

You could also use the following (from - http://docs.python.org/library/itertools.html):

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

(Which depending how you look at it - may or may not be using 'loops' ;))

or something like:

re.findall('..?', string)
2
  • +1 for the zip solution, which none of these other answers mentioned. It actually returns [(str, str), (str, str), ...] though, not [str, str, ...], so it would take more code to make it useful.
    – Mu Mind
    Sep 19, 2012 at 9:26
  • Also, handiest bastardized use of regexes I've ever seen! I like yours the best because it handles odd lengths.
    – Mu Mind
    Sep 19, 2012 at 9:36
0

Yet another solution, this one built on zip and a slice stride:

map(''.join, itertools.izip_longest(mystr[::2], mystr[1::2], fillvalue=''))

It does handle odd-length inputs.

0

Here's a yet another solution without explicit loops (though @Emmanuel's answer is the most appropriate for your question):

s = 'abcdef'
L = zip(s[::2], s[1::2])
# -> [('a', 'b'), ('c', 'd'), ('e', 'f')]

To get strings:

print map(''.join, L)
# ['ab', 'cd', 'ef']

On Python 3 wrap using list() where necessary.

1
  • jinx! we were both thinking zip at the same time
    – Mu Mind
    Sep 19, 2012 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.