Separating IP nodes

Alex Martelli aleaxit at
Mon Sep 4 10:02:08 CEST 2000

"Kevin Breit" <battery841 at> wrote in message
news:SnFs5.137$1U.21639 at elnws01...
> Hey,
> In my app, I have a user enter an IP address.  Great.  Now, I want to
> increment the last number.  For example:
> becomes (yes...thats an odd IP)
> What is the best way to separate that and increment only the last number?
> I'm thinking some sort of regex, but not sure.

The simplest approach (and simplest is often close to best:-) is
to divide the task into steps:
a) split the IP-address string into dot-separated components
b) increment the rightmost number component
c) join the components up again

In pseudocode, using [-1] to indicate the last item of a list and
taking into account integer/string distinctions:
    components = string.split(IPaddress, '.')
    components[-1] = str(1+int(components[-1]))
    IPaddress = string.join(components, '.')

As it happens, this so-called pseudocode is perfectly valid Python
code.  Just slap a 'def inclast(IPaddress):' first line, and a
'return IPaddress' last line, and you have a working Python function
(actually, you'll "return string.join" at the end rather than binding
the result to IPaddress and returning it -- more concise & readable).

Of course, one can do better -- if it's worth it (it normally isn't:
will saving a microsecond's worth of execution time on this function
substantially increase your quality of life...?).  Here, we split
the IP address into 4 parts, while we're only really interested in
_two_ parts -- everything up to the *last* dot, and, everything
*after* it.  In more detail (and, in "pseudocode" again:-)...:
    startOfLast = 1 + string.rindex(IPaddress, '.')
    lastComponent = 1 + int(IPaddress[startOfLast:])
    IPaddress = IPaddress[:startOfLast] + str(lastComponent)
here, too, it's easy to make this 'pseudocode' a working Python function.

I find both solutions equally acceptable in terms of clarity and
directness; which one you come up with will depend on how you
conceptualize the problem.  Performance, if it matters, should be
*MEASURED*, *NOT* "guessed-at": program performance characteristics
more often than not surprise even 'trained' intuition... there is
really no substitute for measurement.  I have a slight preference
for the latter approach, but that may be because it's the _second_
one that came to mind:-); I've done no measurements, because I think
it's unlikely that performance matters here (and no tests, because
I'm a pretty lazy guy:-).

Don't use regular expressions unless you truly need them for clear
and simple solution of a given problem; they're often overkill, when
something, as here, can be handled via string-level manipulation.

Each solution can of course be expressed more compactly (shortening
variable names, merging expressions into one, ...), but there is
really no advantage in this.  The readability and self-documenting
qualities can easily be lost, without compensating advantage.


More information about the Python-list mailing list