How to print every nth index of a python list on a new line?
I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:
[1,2,3,4,5,6,7,8,9,10]
the output would be:
1 2 3 4 5
6 7 8 9 10
I tried this so far:
lst = [1,2,3,4,5,6,7,8,9,10]
for i in lst:
if len(lst) > 5:
print(lst,'n')
but all I get is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.......
How could I do this?
Thanks for the help!
python list printing split
migrated from superuser.com Jan 22 at 5:25
This question came from our site for computer enthusiasts and power users.
add a comment |
I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:
[1,2,3,4,5,6,7,8,9,10]
the output would be:
1 2 3 4 5
6 7 8 9 10
I tried this so far:
lst = [1,2,3,4,5,6,7,8,9,10]
for i in lst:
if len(lst) > 5:
print(lst,'n')
but all I get is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.......
How could I do this?
Thanks for the help!
python list printing split
migrated from superuser.com Jan 22 at 5:25
This question came from our site for computer enthusiasts and power users.
What's your output iflen(list)isn't divisible by your index?
– Primusa
Jan 22 at 5:46
add a comment |
I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:
[1,2,3,4,5,6,7,8,9,10]
the output would be:
1 2 3 4 5
6 7 8 9 10
I tried this so far:
lst = [1,2,3,4,5,6,7,8,9,10]
for i in lst:
if len(lst) > 5:
print(lst,'n')
but all I get is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.......
How could I do this?
Thanks for the help!
python list printing split
I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:
[1,2,3,4,5,6,7,8,9,10]
the output would be:
1 2 3 4 5
6 7 8 9 10
I tried this so far:
lst = [1,2,3,4,5,6,7,8,9,10]
for i in lst:
if len(lst) > 5:
print(lst,'n')
but all I get is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.......
How could I do this?
Thanks for the help!
python list printing split
python list printing split
edited Jan 22 at 5:48
Mehrdad Pedramfar
6,34411543
6,34411543
asked Jan 22 at 5:06
Landon GLandon G
12212
12212
migrated from superuser.com Jan 22 at 5:25
This question came from our site for computer enthusiasts and power users.
migrated from superuser.com Jan 22 at 5:25
This question came from our site for computer enthusiasts and power users.
What's your output iflen(list)isn't divisible by your index?
– Primusa
Jan 22 at 5:46
add a comment |
What's your output iflen(list)isn't divisible by your index?
– Primusa
Jan 22 at 5:46
What's your output if
len(list) isn't divisible by your index?– Primusa
Jan 22 at 5:46
What's your output if
len(list) isn't divisible by your index?– Primusa
Jan 22 at 5:46
add a comment |
4 Answers
4
active
oldest
votes
Try this:
a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)
the output will be:
1 2 3 4 5
6 7 8 9 10
also you can replace 5 with any other number or variables.
Hi, do you mind if I use the*iargument unpacking for the formatting in my answer? It's a lot better than the current" ".join()i've got going on
– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look atsep=inprintfunction too. it is good. something like thisprint(*i, sep='-')it will print:1-2-3-4-5
– Mehrdad Pedramfar
Jan 22 at 5:44
add a comment |
Used a for loop with a step:
n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])
>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]
If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:
1 2 3 4 5
6 7 8 9 10
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
add a comment |
You Can use below code :-
for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))
It will give you desired output.

add a comment |
A bit more idiomatic than the other answers:
n = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for group in zip(*[iter(lst)] * n):
print(*group)
1 2 3 4 5
6 7 8 9 10
For larger lists, it's also much faster:
In [1]: lst = range(1, 10001)
In [2]: n = 5
In [3]: %%timeit
...: for group in zip(*[iter(lst)] * n):
...: group
...:
236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %%timeit
...: for i in range(0, len(lst), n):
...: lst[i:i+n]
...:
1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54301753%2fhow-to-print-every-nth-index-of-a-python-list-on-a-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)
the output will be:
1 2 3 4 5
6 7 8 9 10
also you can replace 5 with any other number or variables.
Hi, do you mind if I use the*iargument unpacking for the formatting in my answer? It's a lot better than the current" ".join()i've got going on
– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look atsep=inprintfunction too. it is good. something like thisprint(*i, sep='-')it will print:1-2-3-4-5
– Mehrdad Pedramfar
Jan 22 at 5:44
add a comment |
Try this:
a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)
the output will be:
1 2 3 4 5
6 7 8 9 10
also you can replace 5 with any other number or variables.
Hi, do you mind if I use the*iargument unpacking for the formatting in my answer? It's a lot better than the current" ".join()i've got going on
– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look atsep=inprintfunction too. it is good. something like thisprint(*i, sep='-')it will print:1-2-3-4-5
– Mehrdad Pedramfar
Jan 22 at 5:44
add a comment |
Try this:
a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)
the output will be:
1 2 3 4 5
6 7 8 9 10
also you can replace 5 with any other number or variables.
Try this:
a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)
the output will be:
1 2 3 4 5
6 7 8 9 10
also you can replace 5 with any other number or variables.
answered Jan 22 at 5:32
Mehrdad PedramfarMehrdad Pedramfar
6,34411543
6,34411543
Hi, do you mind if I use the*iargument unpacking for the formatting in my answer? It's a lot better than the current" ".join()i've got going on
– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look atsep=inprintfunction too. it is good. something like thisprint(*i, sep='-')it will print:1-2-3-4-5
– Mehrdad Pedramfar
Jan 22 at 5:44
add a comment |
Hi, do you mind if I use the*iargument unpacking for the formatting in my answer? It's a lot better than the current" ".join()i've got going on
– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look atsep=inprintfunction too. it is good. something like thisprint(*i, sep='-')it will print:1-2-3-4-5
– Mehrdad Pedramfar
Jan 22 at 5:44
Hi, do you mind if I use the
*i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on– Primusa
Jan 22 at 5:35
Hi, do you mind if I use the
*i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on– Primusa
Jan 22 at 5:35
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
@Primusa Sure it is ok, I will also vote-up your answer.
– Mehrdad Pedramfar
Jan 22 at 5:36
No problem @Primusa, I think that is good if you look at
sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5– Mehrdad Pedramfar
Jan 22 at 5:44
No problem @Primusa, I think that is good if you look at
sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5– Mehrdad Pedramfar
Jan 22 at 5:44
add a comment |
Used a for loop with a step:
n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])
>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]
If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:
1 2 3 4 5
6 7 8 9 10
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
add a comment |
Used a for loop with a step:
n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])
>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]
If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:
1 2 3 4 5
6 7 8 9 10
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
add a comment |
Used a for loop with a step:
n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])
>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]
If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:
1 2 3 4 5
6 7 8 9 10
Used a for loop with a step:
n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])
>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]
If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:
1 2 3 4 5
6 7 8 9 10
edited Jan 22 at 5:37
answered Jan 22 at 5:31
PrimusaPrimusa
8,0002932
8,0002932
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
add a comment |
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)
– Puneet Sinha
Jan 22 at 5:43
add a comment |
You Can use below code :-
for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))
It will give you desired output.

add a comment |
You Can use below code :-
for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))
It will give you desired output.

add a comment |
You Can use below code :-
for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))
It will give you desired output.

You Can use below code :-
for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))
It will give you desired output.

edited Jan 22 at 5:46
Primusa
8,0002932
8,0002932
answered Jan 22 at 5:44
rachit17rachit17
242
242
add a comment |
add a comment |
A bit more idiomatic than the other answers:
n = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for group in zip(*[iter(lst)] * n):
print(*group)
1 2 3 4 5
6 7 8 9 10
For larger lists, it's also much faster:
In [1]: lst = range(1, 10001)
In [2]: n = 5
In [3]: %%timeit
...: for group in zip(*[iter(lst)] * n):
...: group
...:
236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %%timeit
...: for i in range(0, len(lst), n):
...: lst[i:i+n]
...:
1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
add a comment |
A bit more idiomatic than the other answers:
n = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for group in zip(*[iter(lst)] * n):
print(*group)
1 2 3 4 5
6 7 8 9 10
For larger lists, it's also much faster:
In [1]: lst = range(1, 10001)
In [2]: n = 5
In [3]: %%timeit
...: for group in zip(*[iter(lst)] * n):
...: group
...:
236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %%timeit
...: for i in range(0, len(lst), n):
...: lst[i:i+n]
...:
1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
add a comment |
A bit more idiomatic than the other answers:
n = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for group in zip(*[iter(lst)] * n):
print(*group)
1 2 3 4 5
6 7 8 9 10
For larger lists, it's also much faster:
In [1]: lst = range(1, 10001)
In [2]: n = 5
In [3]: %%timeit
...: for group in zip(*[iter(lst)] * n):
...: group
...:
236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %%timeit
...: for i in range(0, len(lst), n):
...: lst[i:i+n]
...:
1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
A bit more idiomatic than the other answers:
n = 5
lst = [1,2,3,4,5,6,7,8,9,10]
for group in zip(*[iter(lst)] * n):
print(*group)
1 2 3 4 5
6 7 8 9 10
For larger lists, it's also much faster:
In [1]: lst = range(1, 10001)
In [2]: n = 5
In [3]: %%timeit
...: for group in zip(*[iter(lst)] * n):
...: group
...:
236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %%timeit
...: for i in range(0, len(lst), n):
...: lst[i:i+n]
...:
1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
edited Jan 22 at 5:48
answered Jan 22 at 5:39
Tomothy32Tomothy32
7,8461728
7,8461728
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54301753%2fhow-to-print-every-nth-index-of-a-python-list-on-a-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's your output if
len(list)isn't divisible by your index?– Primusa
Jan 22 at 5:46