why can't I print a single tuple from a list?
I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)
However if I make this simple edit, it works fine. why can't I print a single tuple from a list?
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)
python
add a comment |
I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)
However if I make this simple edit, it works fine. why can't I print a single tuple from a list?
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)
python
3
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
4
Because inlst[0]
you index list by integer which returns single element however inlst[:1]
you index list by slice which returns list that you can iterate over.
– Filip Młynarski
Nov 23 '18 at 18:04
1
print(lst[0][1], lst[0][0])
.
– Austin
Nov 23 '18 at 18:05
add a comment |
I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)
However if I make this simple edit, it works fine. why can't I print a single tuple from a list?
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)
python
I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)
However if I make this simple edit, it works fine. why can't I print a single tuple from a list?
lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)
python
python
edited Nov 23 '18 at 18:05
slider
8,08011129
8,08011129
asked Nov 23 '18 at 18:02
xgeniux
245
245
3
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
4
Because inlst[0]
you index list by integer which returns single element however inlst[:1]
you index list by slice which returns list that you can iterate over.
– Filip Młynarski
Nov 23 '18 at 18:04
1
print(lst[0][1], lst[0][0])
.
– Austin
Nov 23 '18 at 18:05
add a comment |
3
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
4
Because inlst[0]
you index list by integer which returns single element however inlst[:1]
you index list by slice which returns list that you can iterate over.
– Filip Młynarski
Nov 23 '18 at 18:04
1
print(lst[0][1], lst[0][0])
.
– Austin
Nov 23 '18 at 18:05
3
3
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
4
4
Because in
lst[0]
you index list by integer which returns single element however in lst[:1]
you index list by slice which returns list that you can iterate over.– Filip Młynarski
Nov 23 '18 at 18:04
Because in
lst[0]
you index list by integer which returns single element however in lst[:1]
you index list by slice which returns list that you can iterate over.– Filip Młynarski
Nov 23 '18 at 18:04
1
1
print(lst[0][1], lst[0][0])
.– Austin
Nov 23 '18 at 18:05
print(lst[0][1], lst[0][0])
.– Austin
Nov 23 '18 at 18:05
add a comment |
4 Answers
4
active
oldest
votes
Easiest way to flip first element would be to:
lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')
However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1)
by iterating over it first value is 'a'
and you try to split it into 2 variables x
and y
which throws an error.
print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)
Here if we iterate over list [('a',1)]
(because we used slice as list index instead of integer) first value is indeed ('a',1)
and we can split it into x
and y
variables without errors.
print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)
add a comment |
Your list:
lst = [('a',1),('b',2),('c',3)]
The first element of the list:
>>> lst[0]
(1, 'a')
Then when you iterate over this, you are asking to unpack each element. It would be like writing
for x, y in 1:
# do something
for x, y in 'a':
# do something
Wth lst[:1]
, you are slicing the list, and getting a list of tuples back.
add a comment |
You are trying to loop through the first element of the list.
Just unzip the first element and print:
x, y = lst[0]
print(y,x)
or in a loop:
for x, y in lst:
print(y, x)
add a comment |
You don't want a loop there. lst[0]
is the tuple you want to flip, there is no need to iterate.
x, y = lst[0]
print(y, X)
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%2f53451194%2fwhy-cant-i-print-a-single-tuple-from-a-list%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
Easiest way to flip first element would be to:
lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')
However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1)
by iterating over it first value is 'a'
and you try to split it into 2 variables x
and y
which throws an error.
print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)
Here if we iterate over list [('a',1)]
(because we used slice as list index instead of integer) first value is indeed ('a',1)
and we can split it into x
and y
variables without errors.
print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)
add a comment |
Easiest way to flip first element would be to:
lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')
However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1)
by iterating over it first value is 'a'
and you try to split it into 2 variables x
and y
which throws an error.
print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)
Here if we iterate over list [('a',1)]
(because we used slice as list index instead of integer) first value is indeed ('a',1)
and we can split it into x
and y
variables without errors.
print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)
add a comment |
Easiest way to flip first element would be to:
lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')
However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1)
by iterating over it first value is 'a'
and you try to split it into 2 variables x
and y
which throws an error.
print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)
Here if we iterate over list [('a',1)]
(because we used slice as list index instead of integer) first value is indeed ('a',1)
and we can split it into x
and y
variables without errors.
print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)
Easiest way to flip first element would be to:
lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')
However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1)
by iterating over it first value is 'a'
and you try to split it into 2 variables x
and y
which throws an error.
print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)
Here if we iterate over list [('a',1)]
(because we used slice as list index instead of integer) first value is indeed ('a',1)
and we can split it into x
and y
variables without errors.
print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)
answered Nov 23 '18 at 18:15
Filip Młynarski
1,5931311
1,5931311
add a comment |
add a comment |
Your list:
lst = [('a',1),('b',2),('c',3)]
The first element of the list:
>>> lst[0]
(1, 'a')
Then when you iterate over this, you are asking to unpack each element. It would be like writing
for x, y in 1:
# do something
for x, y in 'a':
# do something
Wth lst[:1]
, you are slicing the list, and getting a list of tuples back.
add a comment |
Your list:
lst = [('a',1),('b',2),('c',3)]
The first element of the list:
>>> lst[0]
(1, 'a')
Then when you iterate over this, you are asking to unpack each element. It would be like writing
for x, y in 1:
# do something
for x, y in 'a':
# do something
Wth lst[:1]
, you are slicing the list, and getting a list of tuples back.
add a comment |
Your list:
lst = [('a',1),('b',2),('c',3)]
The first element of the list:
>>> lst[0]
(1, 'a')
Then when you iterate over this, you are asking to unpack each element. It would be like writing
for x, y in 1:
# do something
for x, y in 'a':
# do something
Wth lst[:1]
, you are slicing the list, and getting a list of tuples back.
Your list:
lst = [('a',1),('b',2),('c',3)]
The first element of the list:
>>> lst[0]
(1, 'a')
Then when you iterate over this, you are asking to unpack each element. It would be like writing
for x, y in 1:
# do something
for x, y in 'a':
# do something
Wth lst[:1]
, you are slicing the list, and getting a list of tuples back.
answered Nov 23 '18 at 18:05
JETM
1,89241627
1,89241627
add a comment |
add a comment |
You are trying to loop through the first element of the list.
Just unzip the first element and print:
x, y = lst[0]
print(y,x)
or in a loop:
for x, y in lst:
print(y, x)
add a comment |
You are trying to loop through the first element of the list.
Just unzip the first element and print:
x, y = lst[0]
print(y,x)
or in a loop:
for x, y in lst:
print(y, x)
add a comment |
You are trying to loop through the first element of the list.
Just unzip the first element and print:
x, y = lst[0]
print(y,x)
or in a loop:
for x, y in lst:
print(y, x)
You are trying to loop through the first element of the list.
Just unzip the first element and print:
x, y = lst[0]
print(y,x)
or in a loop:
for x, y in lst:
print(y, x)
edited Nov 23 '18 at 18:21
handras
410115
410115
answered Nov 23 '18 at 18:05
Attila Bognár
1226
1226
add a comment |
add a comment |
You don't want a loop there. lst[0]
is the tuple you want to flip, there is no need to iterate.
x, y = lst[0]
print(y, X)
add a comment |
You don't want a loop there. lst[0]
is the tuple you want to flip, there is no need to iterate.
x, y = lst[0]
print(y, X)
add a comment |
You don't want a loop there. lst[0]
is the tuple you want to flip, there is no need to iterate.
x, y = lst[0]
print(y, X)
You don't want a loop there. lst[0]
is the tuple you want to flip, there is no need to iterate.
x, y = lst[0]
print(y, X)
answered Nov 23 '18 at 18:05
Daniel Roseman
444k41576632
444k41576632
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.
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%2f53451194%2fwhy-cant-i-print-a-single-tuple-from-a-list%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
Why are you iterating?
– user2357112
Nov 23 '18 at 18:03
4
Because in
lst[0]
you index list by integer which returns single element however inlst[:1]
you index list by slice which returns list that you can iterate over.– Filip Młynarski
Nov 23 '18 at 18:04
1
print(lst[0][1], lst[0][0])
.– Austin
Nov 23 '18 at 18:05