Divide a list in the following manner [duplicate]

Multi tool use
This question already has an answer here:
Iterate over all pairs of consecutive items in a list
6 answers
I have this list
list=[0,1,2,3,4,2,6]
I want to make another list of tuples like this:
list=[(0,1),(1,2),(2,3),(3,4),(4,2),(2,6)]
Is there a built-in function that i can use?
python list
marked as duplicate by Daniel Mesejo, Michael Butscher, juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 1:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Iterate over all pairs of consecutive items in a list
6 answers
I have this list
list=[0,1,2,3,4,2,6]
I want to make another list of tuples like this:
list=[(0,1),(1,2),(2,3),(3,4),(4,2),(2,6)]
Is there a built-in function that i can use?
python list
marked as duplicate by Daniel Mesejo, Michael Butscher, juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 1:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41
add a comment |
This question already has an answer here:
Iterate over all pairs of consecutive items in a list
6 answers
I have this list
list=[0,1,2,3,4,2,6]
I want to make another list of tuples like this:
list=[(0,1),(1,2),(2,3),(3,4),(4,2),(2,6)]
Is there a built-in function that i can use?
python list
This question already has an answer here:
Iterate over all pairs of consecutive items in a list
6 answers
I have this list
list=[0,1,2,3,4,2,6]
I want to make another list of tuples like this:
list=[(0,1),(1,2),(2,3),(3,4),(4,2),(2,6)]
Is there a built-in function that i can use?
This question already has an answer here:
Iterate over all pairs of consecutive items in a list
6 answers
python list
python list
edited Dec 28 '18 at 0:40


Daniel Mesejo
14.2k11027
14.2k11027
asked Dec 28 '18 at 0:35


loyal Turkman
15
15
marked as duplicate by Daniel Mesejo, Michael Butscher, juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 1:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Daniel Mesejo, Michael Butscher, juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 1:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41
add a comment |
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41
add a comment |
3 Answers
3
active
oldest
votes
You can use zip and list comprehension. listt[1:]
starts from the second element. This is done because in your first pair, you have the 1st and the 2nd element. zip
creates pairs and then you iterate over them and use ()
to store them as tuples.
A piece of advice: Do not use builtin names as variables. list
in your case.
listt=[0,1,2,3,4,2,6]
result = [(i) for i in zip(listt, listt[1:])]
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
Benchmarking performance
Based on @prashant rana's comment, I compared the performance of zip
with his approach taking a list 1 million times longer than the original list. Below are the results: zip
turns out to be faster
import timeit
listt=[0,1,2,3,4,2,6]*1000000
%timeit [(listt[i],listt[i+1]) for i in range(len(listt)-1)]
%timeit [(i) for i in zip(listt, listt[1:])]
1.76 s ± 178 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
937 ms ± 46.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.zip
is faster than your method
– Bazingaa
Dec 28 '18 at 1:00
yeszip
really is more faster.
– prashant rana
Dec 28 '18 at 1:16
add a comment |
[(i, list[idx + 1]) for idx, i in enumerate(list) if idx < len(list) - 1]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
add a comment |
There's no builtin function, but python makes it very easy to make your own. Here you go:
list = [0, 1, 2, 3, 4, 2, 6]
x = [(list[i], list[i+1]) for i in range(len(list)-1)]
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use zip and list comprehension. listt[1:]
starts from the second element. This is done because in your first pair, you have the 1st and the 2nd element. zip
creates pairs and then you iterate over them and use ()
to store them as tuples.
A piece of advice: Do not use builtin names as variables. list
in your case.
listt=[0,1,2,3,4,2,6]
result = [(i) for i in zip(listt, listt[1:])]
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
Benchmarking performance
Based on @prashant rana's comment, I compared the performance of zip
with his approach taking a list 1 million times longer than the original list. Below are the results: zip
turns out to be faster
import timeit
listt=[0,1,2,3,4,2,6]*1000000
%timeit [(listt[i],listt[i+1]) for i in range(len(listt)-1)]
%timeit [(i) for i in zip(listt, listt[1:])]
1.76 s ± 178 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
937 ms ± 46.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.zip
is faster than your method
– Bazingaa
Dec 28 '18 at 1:00
yeszip
really is more faster.
– prashant rana
Dec 28 '18 at 1:16
add a comment |
You can use zip and list comprehension. listt[1:]
starts from the second element. This is done because in your first pair, you have the 1st and the 2nd element. zip
creates pairs and then you iterate over them and use ()
to store them as tuples.
A piece of advice: Do not use builtin names as variables. list
in your case.
listt=[0,1,2,3,4,2,6]
result = [(i) for i in zip(listt, listt[1:])]
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
Benchmarking performance
Based on @prashant rana's comment, I compared the performance of zip
with his approach taking a list 1 million times longer than the original list. Below are the results: zip
turns out to be faster
import timeit
listt=[0,1,2,3,4,2,6]*1000000
%timeit [(listt[i],listt[i+1]) for i in range(len(listt)-1)]
%timeit [(i) for i in zip(listt, listt[1:])]
1.76 s ± 178 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
937 ms ± 46.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.zip
is faster than your method
– Bazingaa
Dec 28 '18 at 1:00
yeszip
really is more faster.
– prashant rana
Dec 28 '18 at 1:16
add a comment |
You can use zip and list comprehension. listt[1:]
starts from the second element. This is done because in your first pair, you have the 1st and the 2nd element. zip
creates pairs and then you iterate over them and use ()
to store them as tuples.
A piece of advice: Do not use builtin names as variables. list
in your case.
listt=[0,1,2,3,4,2,6]
result = [(i) for i in zip(listt, listt[1:])]
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
Benchmarking performance
Based on @prashant rana's comment, I compared the performance of zip
with his approach taking a list 1 million times longer than the original list. Below are the results: zip
turns out to be faster
import timeit
listt=[0,1,2,3,4,2,6]*1000000
%timeit [(listt[i],listt[i+1]) for i in range(len(listt)-1)]
%timeit [(i) for i in zip(listt, listt[1:])]
1.76 s ± 178 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
937 ms ± 46.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
You can use zip and list comprehension. listt[1:]
starts from the second element. This is done because in your first pair, you have the 1st and the 2nd element. zip
creates pairs and then you iterate over them and use ()
to store them as tuples.
A piece of advice: Do not use builtin names as variables. list
in your case.
listt=[0,1,2,3,4,2,6]
result = [(i) for i in zip(listt, listt[1:])]
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
Benchmarking performance
Based on @prashant rana's comment, I compared the performance of zip
with his approach taking a list 1 million times longer than the original list. Below are the results: zip
turns out to be faster
import timeit
listt=[0,1,2,3,4,2,6]*1000000
%timeit [(listt[i],listt[i+1]) for i in range(len(listt)-1)]
%timeit [(i) for i in zip(listt, listt[1:])]
1.76 s ± 178 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
937 ms ± 46.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
edited Dec 28 '18 at 0:59
answered Dec 28 '18 at 0:39


Bazingaa
11.6k21125
11.6k21125
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.zip
is faster than your method
– Bazingaa
Dec 28 '18 at 1:00
yeszip
really is more faster.
– prashant rana
Dec 28 '18 at 1:16
add a comment |
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.zip
is faster than your method
– Bazingaa
Dec 28 '18 at 1:00
yeszip
really is more faster.
– prashant rana
Dec 28 '18 at 1:16
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
using zip will make it slow .
– prashant rana
Dec 28 '18 at 0:43
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
Might be true but I am assuming the OP is not using it for production purposes. For small lists, like in the question, it serves the purpose
– Bazingaa
Dec 28 '18 at 0:44
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
i find it's execution time twice the execution time of mine
– prashant rana
Dec 28 '18 at 0:53
2
2
@prashantrana: Check my edit for a list 1 million times larger than the original list.
zip
is faster than your method– Bazingaa
Dec 28 '18 at 1:00
@prashantrana: Check my edit for a list 1 million times larger than the original list.
zip
is faster than your method– Bazingaa
Dec 28 '18 at 1:00
yes
zip
really is more faster.– prashant rana
Dec 28 '18 at 1:16
yes
zip
really is more faster.– prashant rana
Dec 28 '18 at 1:16
add a comment |
[(i, list[idx + 1]) for idx, i in enumerate(list) if idx < len(list) - 1]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
add a comment |
[(i, list[idx + 1]) for idx, i in enumerate(list) if idx < len(list) - 1]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
add a comment |
[(i, list[idx + 1]) for idx, i in enumerate(list) if idx < len(list) - 1]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
[(i, list[idx + 1]) for idx, i in enumerate(list) if idx < len(list) - 1]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 2), (2, 6)]
answered Dec 28 '18 at 0:40


ic3b3rg
10.6k41945
10.6k41945
add a comment |
add a comment |
There's no builtin function, but python makes it very easy to make your own. Here you go:
list = [0, 1, 2, 3, 4, 2, 6]
x = [(list[i], list[i+1]) for i in range(len(list)-1)]
add a comment |
There's no builtin function, but python makes it very easy to make your own. Here you go:
list = [0, 1, 2, 3, 4, 2, 6]
x = [(list[i], list[i+1]) for i in range(len(list)-1)]
add a comment |
There's no builtin function, but python makes it very easy to make your own. Here you go:
list = [0, 1, 2, 3, 4, 2, 6]
x = [(list[i], list[i+1]) for i in range(len(list)-1)]
There's no builtin function, but python makes it very easy to make your own. Here you go:
list = [0, 1, 2, 3, 4, 2, 6]
x = [(list[i], list[i+1]) for i in range(len(list)-1)]
edited Dec 28 '18 at 0:55


Ethan K
461218
461218
answered Dec 28 '18 at 0:40
JimmyCarlos
417215
417215
add a comment |
add a comment |
I8dQz6j6
use zip
– Daniel Mesejo
Dec 28 '18 at 0:38
Do not use list as a variable name, it shadows the built-int list function
– Daniel Mesejo
Dec 28 '18 at 0:41