Python - List returning [[…], 6]
If I run the following code
data = [[1,2],[3,4],[5,6]]
for x in data:
print(x[0])
for x[0] in data:
print(x)
I get the following output
1
3
5
[[1, 2], 6]
[[3, 4], 6]
[[...], 6]
I end up with a list containing [[...], 6]
, but what is this [...]
list?
It doesn't behave normally, because calling y = [[...], 6]
and then the following statements show [...]
to be 0
>>> print(y)
[[Ellipsis], 6]
>>> print(y[0])
[0]
However when I run the code at the top, and type the following statements the results don't make sense:
>>> print(x)
[[...], 6]
>>> print(x[0])
[[...], 6]
>>> print(x[0][0])
[[...], 6]
>>> print(x[0][0][0])
[[...], 6]
and yet somehow both of these result in 6
>>> print(x[1])
6
>>> print(x[0][1])
6
To review the question: How is this possible, and what does [...]
represent, and how can the for loop at the top create such a list?
python python-3.x
add a comment |
If I run the following code
data = [[1,2],[3,4],[5,6]]
for x in data:
print(x[0])
for x[0] in data:
print(x)
I get the following output
1
3
5
[[1, 2], 6]
[[3, 4], 6]
[[...], 6]
I end up with a list containing [[...], 6]
, but what is this [...]
list?
It doesn't behave normally, because calling y = [[...], 6]
and then the following statements show [...]
to be 0
>>> print(y)
[[Ellipsis], 6]
>>> print(y[0])
[0]
However when I run the code at the top, and type the following statements the results don't make sense:
>>> print(x)
[[...], 6]
>>> print(x[0])
[[...], 6]
>>> print(x[0][0])
[[...], 6]
>>> print(x[0][0][0])
[[...], 6]
and yet somehow both of these result in 6
>>> print(x[1])
6
>>> print(x[0][1])
6
To review the question: How is this possible, and what does [...]
represent, and how can the for loop at the top create such a list?
python python-3.x
3
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Because each step of the loop assigns the next element fromx
tox[0]
. Those two loops are not the same.
– jonrsharpe
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday
add a comment |
If I run the following code
data = [[1,2],[3,4],[5,6]]
for x in data:
print(x[0])
for x[0] in data:
print(x)
I get the following output
1
3
5
[[1, 2], 6]
[[3, 4], 6]
[[...], 6]
I end up with a list containing [[...], 6]
, but what is this [...]
list?
It doesn't behave normally, because calling y = [[...], 6]
and then the following statements show [...]
to be 0
>>> print(y)
[[Ellipsis], 6]
>>> print(y[0])
[0]
However when I run the code at the top, and type the following statements the results don't make sense:
>>> print(x)
[[...], 6]
>>> print(x[0])
[[...], 6]
>>> print(x[0][0])
[[...], 6]
>>> print(x[0][0][0])
[[...], 6]
and yet somehow both of these result in 6
>>> print(x[1])
6
>>> print(x[0][1])
6
To review the question: How is this possible, and what does [...]
represent, and how can the for loop at the top create such a list?
python python-3.x
If I run the following code
data = [[1,2],[3,4],[5,6]]
for x in data:
print(x[0])
for x[0] in data:
print(x)
I get the following output
1
3
5
[[1, 2], 6]
[[3, 4], 6]
[[...], 6]
I end up with a list containing [[...], 6]
, but what is this [...]
list?
It doesn't behave normally, because calling y = [[...], 6]
and then the following statements show [...]
to be 0
>>> print(y)
[[Ellipsis], 6]
>>> print(y[0])
[0]
However when I run the code at the top, and type the following statements the results don't make sense:
>>> print(x)
[[...], 6]
>>> print(x[0])
[[...], 6]
>>> print(x[0][0])
[[...], 6]
>>> print(x[0][0][0])
[[...], 6]
and yet somehow both of these result in 6
>>> print(x[1])
6
>>> print(x[0][1])
6
To review the question: How is this possible, and what does [...]
represent, and how can the for loop at the top create such a list?
python python-3.x
python python-3.x
edited yesterday
Mad Physicist
34.2k156895
34.2k156895
asked yesterday
Ruler Of The WorldRuler Of The World
502318
502318
3
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Because each step of the loop assigns the next element fromx
tox[0]
. Those two loops are not the same.
– jonrsharpe
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday
add a comment |
3
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Because each step of the loop assigns the next element fromx
tox[0]
. Those two loops are not the same.
– jonrsharpe
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday
3
3
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Because each step of the loop assigns the next element from
x
to x[0]
. Those two loops are not the same.– jonrsharpe
yesterday
Because each step of the loop assigns the next element from
x
to x[0]
. Those two loops are not the same.– jonrsharpe
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday
add a comment |
2 Answers
2
active
oldest
votes
Let's give your sublists names:
a = [1, 2]
b = [3, 4]
c = [5, 6]
data = [a, b, c]
Your first loop binds a
, b
and c
successively to x
. When the loop terminates, you have effectively set x = c
.
The second loop now binds a
, b
and c
successively to x[0]
. This is fine for a
and b
, but for c
you are effectively doing c[0] = c
, creating a circular reference. Since list
is able to catch that, it won't try to print [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
...
add a comment |
that's because you're using x[0]
as your loop variable (which is bad practice) which exists as a list and not a new name like you're supposed to when iterating with for
for x[0] in data:
print(x)
and x
is in data
so there's a cyclic reference (hence the ellipsis representation to avoid infinite recursion when printing the same data over and over)
More in detail:
The ellipsis happens on the last element because of the previous loop that binds x
on the last element of data
([5,6]
).
So the second loop assigns [5,6]
to x[0]
but it's also x
. On way to get rid of this is to create a copy of x
just before the second loop: x = x[:]
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.
– Jean-François Fabre
yesterday
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%2f54063064%2fpython-list-returning-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's give your sublists names:
a = [1, 2]
b = [3, 4]
c = [5, 6]
data = [a, b, c]
Your first loop binds a
, b
and c
successively to x
. When the loop terminates, you have effectively set x = c
.
The second loop now binds a
, b
and c
successively to x[0]
. This is fine for a
and b
, but for c
you are effectively doing c[0] = c
, creating a circular reference. Since list
is able to catch that, it won't try to print [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
...
add a comment |
Let's give your sublists names:
a = [1, 2]
b = [3, 4]
c = [5, 6]
data = [a, b, c]
Your first loop binds a
, b
and c
successively to x
. When the loop terminates, you have effectively set x = c
.
The second loop now binds a
, b
and c
successively to x[0]
. This is fine for a
and b
, but for c
you are effectively doing c[0] = c
, creating a circular reference. Since list
is able to catch that, it won't try to print [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
...
add a comment |
Let's give your sublists names:
a = [1, 2]
b = [3, 4]
c = [5, 6]
data = [a, b, c]
Your first loop binds a
, b
and c
successively to x
. When the loop terminates, you have effectively set x = c
.
The second loop now binds a
, b
and c
successively to x[0]
. This is fine for a
and b
, but for c
you are effectively doing c[0] = c
, creating a circular reference. Since list
is able to catch that, it won't try to print [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
...
Let's give your sublists names:
a = [1, 2]
b = [3, 4]
c = [5, 6]
data = [a, b, c]
Your first loop binds a
, b
and c
successively to x
. When the loop terminates, you have effectively set x = c
.
The second loop now binds a
, b
and c
successively to x[0]
. This is fine for a
and b
, but for c
you are effectively doing c[0] = c
, creating a circular reference. Since list
is able to catch that, it won't try to print [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
...
answered yesterday
Mad PhysicistMad Physicist
34.2k156895
34.2k156895
add a comment |
add a comment |
that's because you're using x[0]
as your loop variable (which is bad practice) which exists as a list and not a new name like you're supposed to when iterating with for
for x[0] in data:
print(x)
and x
is in data
so there's a cyclic reference (hence the ellipsis representation to avoid infinite recursion when printing the same data over and over)
More in detail:
The ellipsis happens on the last element because of the previous loop that binds x
on the last element of data
([5,6]
).
So the second loop assigns [5,6]
to x[0]
but it's also x
. On way to get rid of this is to create a copy of x
just before the second loop: x = x[:]
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.
– Jean-François Fabre
yesterday
add a comment |
that's because you're using x[0]
as your loop variable (which is bad practice) which exists as a list and not a new name like you're supposed to when iterating with for
for x[0] in data:
print(x)
and x
is in data
so there's a cyclic reference (hence the ellipsis representation to avoid infinite recursion when printing the same data over and over)
More in detail:
The ellipsis happens on the last element because of the previous loop that binds x
on the last element of data
([5,6]
).
So the second loop assigns [5,6]
to x[0]
but it's also x
. On way to get rid of this is to create a copy of x
just before the second loop: x = x[:]
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.
– Jean-François Fabre
yesterday
add a comment |
that's because you're using x[0]
as your loop variable (which is bad practice) which exists as a list and not a new name like you're supposed to when iterating with for
for x[0] in data:
print(x)
and x
is in data
so there's a cyclic reference (hence the ellipsis representation to avoid infinite recursion when printing the same data over and over)
More in detail:
The ellipsis happens on the last element because of the previous loop that binds x
on the last element of data
([5,6]
).
So the second loop assigns [5,6]
to x[0]
but it's also x
. On way to get rid of this is to create a copy of x
just before the second loop: x = x[:]
that's because you're using x[0]
as your loop variable (which is bad practice) which exists as a list and not a new name like you're supposed to when iterating with for
for x[0] in data:
print(x)
and x
is in data
so there's a cyclic reference (hence the ellipsis representation to avoid infinite recursion when printing the same data over and over)
More in detail:
The ellipsis happens on the last element because of the previous loop that binds x
on the last element of data
([5,6]
).
So the second loop assigns [5,6]
to x[0]
but it's also x
. On way to get rid of this is to create a copy of x
just before the second loop: x = x[:]
edited yesterday
answered yesterday
Jean-François FabreJean-François Fabre
101k954109
101k954109
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.
– Jean-François Fabre
yesterday
add a comment |
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.
– Jean-François Fabre
yesterday
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
Great answer, so the […] represents an infinite loop of the same list?
– Ruler Of The World
yesterday
...
is the way to represent something that cycles.– Jean-François Fabre
yesterday
...
is the way to represent something that cycles.– Jean-François Fabre
yesterday
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f54063064%2fpython-list-returning-6%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
3
Did you do any research? stackoverflow.com/questions/17160162/…
– jonrsharpe
yesterday
Strangely I couldn't find that question from my searches, it seems to answer the question perfectly. The only question that remains unanswered is how does the for loop create such a list? @jonrsharpe
– Ruler Of The World
yesterday
Because each step of the loop assigns the next element from
x
tox[0]
. Those two loops are not the same.– jonrsharpe
yesterday
A very worthy Hot Network Question. Here's another useful question that compliments the one linked above by Jon: stackoverflow.com/questions/30462352/…
– vaultah
yesterday