3

Consider the below list in Python.

c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

I want to create n strings where n = len(c)/6. So for this example, the expected output would be:

str1=1|2|3|4|5|6
str2=7|8|9|10|11|12
str3=13|14|15|16|17|18

How do I do it using loops? Note that the length of c will always be a multiple of 6.

6 Answers 6

6

Use a comprehension to generate the sub-list chunks, and then unpack into variables:

c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
str1, str2, str3 = (c[i:i+6] for i in range(0, len(c), 6))

Or, if you actually want strings like: '1|2|3|4|5|6' then you can use str.join on the sub-lists but this will require converting everything to strings first:

str1, str2, str3 = ('|'.join(map(str, c[i:i+6])) for i in range(0, len(c), 6))

which gives:

>>> str1
'1|2|3|4|5|6'
>>> str2
'7|8|9|10|11|12'
>>> str3
'13|14|15|16|17|18'

Note that in the second snippet, you could use a generator-expression instead of map() which is usually considered more Pythonic, but they take up more characters for simple things (like converting to strings).

Sign up to request clarification or add additional context in comments.

4 Comments

Oops didn't see your answer. I'll delete mine.
@RoadRunner No problem! Happens to me all the time, turns out I actually wrote another part to mine (the string mapping bit) whilst you were writing yours :)
yeah, happens all too often. Your answer is 10 x better anyways because it explains the process. I'll give the upvote to you and delete mine :).
@RoadRunner Thanks :)
1
>>> c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
>>> str1, str2, str3 = map('|'.join, zip(*[map(str, c)]*6))
>>> str1, str2, str3
('1|2|3|4|5|6', '7|8|9|10|11|12', '13|14|15|16|17|18')

On Python 2 use from itertools import imap


If you use more_itertools library you can look even nicer:

>>> from more_itertools import grouper
>>> c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
>>> str1, str2, str3 = map('|'.join, grouper(6, map(str, c)))
>>> str1, str2, str3
('1|2|3|4|5|6', '7|8|9|10|11|12', '13|14|15|16|17|18')

Comments

1

You can use recursion :

c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

multiple=len(c)/6

def hello(c1,g1):

    if not c1:
        return 0
    else:
        print(c1[:g1])
        return hello(c1[g1:],g1)
print(hello(c,int(len(c)/multiple)))

output:

[1, 2, 3, 4, 5, 6]
[7, 8, 9, 10, 11, 12]
[13, 14, 15, 16, 17, 18]

Comments

0

You can also use join here:

c=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

n = len(c)/3
for i in range(0,len(c),n):
    print "|".join(str(x) for x in c[i:i+n])

Comments

0

Using a for loop would be something like:

strings = []
sl_sz = 6
for i in range(0, len(c) / sl_sz):
    strings.append("|".join(map(str, c[i*sl_sz:(i + 1)*sl_sz])))

1 Comment

You can set the step value of range to 6, and then the code will become much neater. Also, your lambda function is unnecessary, it literally carries out str() so just do: map(str, ...)!
0

Another way of doing it is by using islice from itertools module:

from itertools import islice

c = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

str1, str2, str3 = ["|".join(map(str, list(islice(c,i*6,i*6+6)))) for i in range(len(c)//6)]

print(str1)
print(str2)
print(str3)

Output:

1|2|3|4|5|6
7|8|9|10|11|12
13|14|15|16|17|18

Comments

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.