Katkat every 4 lines from the list?

I have this list:

['192', '168', '0', '1', '80', '192', '168', '0', '2', '8080']...

And I want to get this list:

['192.168.0.1:80', '192.168.0.2:8080']...

What is the best way to do this?

using rangewith listpop?

using listslicing?

+3
source share
2 answers
>>> data = ['192', '168', '0', '1', '80', '192', '168', '0', '2', '8080']
>>> ['{}.{}.{}.{}:{}'.format(*x) for x in zip(*[iter(data)]*5)]
['192.168.0.1:80', '192.168.0.2:8080']

Using starmap

>>> from itertools import starmap
>>> list(starmap('{}.{}.{}.{}:{}'.format,zip(*[iter(data)]*5)))
['192.168.0.1:80', '192.168.0.2:8080']
+5
source

This is one way to do this, which may or may not be the best way:

>>> a = ['192', '168', '0', '1', '80', '192', '168', '0', '2', '8080']
>>> [":".join([".".join(a[x:x+4]), a[x+4]]) for x in range(0, len(a), 5)]
['192.168.0.1:80', '192.168.0.2:8080']
+2
source

All Articles