Length of a single sublist in python
Lets assume i have a list of 3 sublists
a = [[1,1,1],[1,1,1],[1,1,1]]
If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)
But when I do len(a[2])
and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).
How could I solve this problem?
python list sublist
add a comment |
Lets assume i have a list of 3 sublists
a = [[1,1,1],[1,1,1],[1,1,1]]
If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)
But when I do len(a[2])
and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).
How could I solve this problem?
python list sublist
How aboutlen(a[2:3])
?
– Loocid
Nov 23 '18 at 1:13
len(a[2])
takes the length of the third element.len(a[0:2])
takes the number of elements in that range.
– Cua
Nov 23 '18 at 2:07
add a comment |
Lets assume i have a list of 3 sublists
a = [[1,1,1],[1,1,1],[1,1,1]]
If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)
But when I do len(a[2])
and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).
How could I solve this problem?
python list sublist
Lets assume i have a list of 3 sublists
a = [[1,1,1],[1,1,1],[1,1,1]]
If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)
But when I do len(a[2])
and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).
How could I solve this problem?
python list sublist
python list sublist
asked Nov 23 '18 at 1:09
Kaur Kadak
143
143
How aboutlen(a[2:3])
?
– Loocid
Nov 23 '18 at 1:13
len(a[2])
takes the length of the third element.len(a[0:2])
takes the number of elements in that range.
– Cua
Nov 23 '18 at 2:07
add a comment |
How aboutlen(a[2:3])
?
– Loocid
Nov 23 '18 at 1:13
len(a[2])
takes the length of the third element.len(a[0:2])
takes the number of elements in that range.
– Cua
Nov 23 '18 at 2:07
How about
len(a[2:3])
?– Loocid
Nov 23 '18 at 1:13
How about
len(a[2:3])
?– Loocid
Nov 23 '18 at 1:13
len(a[2])
takes the length of the third element. len(a[0:2])
takes the number of elements in that range.– Cua
Nov 23 '18 at 2:07
len(a[2])
takes the length of the third element. len(a[0:2])
takes the number of elements in that range.– Cua
Nov 23 '18 at 2:07
add a comment |
3 Answers
3
active
oldest
votes
You need to use len(a[2:3])
:
a = [[1,2,3],[4,5,6],[7,8,9]]
a[2]
>>> [7, 8, 9]
len(a[2])
>>> 3
a[2:3]
>>> [[7, 8, 9]]
len(a[2:3])
>>> 1
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
add a comment |
You have to specify the range you want to look at.
len(a[1:2])
should do the trick.
len(a[x:y])
simply means "the length of a from element x to y (non-inclusive)"
So if you do len(a[2:2])
the output is 0.
add a comment |
You can use:
print(len(a[2:3]))
Or if want it builtin, do a function:
_len=len
def len(l):
if _len(l)==0:
return 0
elif isinstance(l[0],list):
return _len(l)
return _len([l])
a=[[1,1,1],[1,1,1],[1,1,1]]
print(len(a[2]))
Both Output:
1
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%2f53439591%2flength-of-a-single-sublist-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use len(a[2:3])
:
a = [[1,2,3],[4,5,6],[7,8,9]]
a[2]
>>> [7, 8, 9]
len(a[2])
>>> 3
a[2:3]
>>> [[7, 8, 9]]
len(a[2:3])
>>> 1
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
add a comment |
You need to use len(a[2:3])
:
a = [[1,2,3],[4,5,6],[7,8,9]]
a[2]
>>> [7, 8, 9]
len(a[2])
>>> 3
a[2:3]
>>> [[7, 8, 9]]
len(a[2:3])
>>> 1
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
add a comment |
You need to use len(a[2:3])
:
a = [[1,2,3],[4,5,6],[7,8,9]]
a[2]
>>> [7, 8, 9]
len(a[2])
>>> 3
a[2:3]
>>> [[7, 8, 9]]
len(a[2:3])
>>> 1
You need to use len(a[2:3])
:
a = [[1,2,3],[4,5,6],[7,8,9]]
a[2]
>>> [7, 8, 9]
len(a[2])
>>> 3
a[2:3]
>>> [[7, 8, 9]]
len(a[2:3])
>>> 1
answered Nov 23 '18 at 1:18
Julian Peller
864511
864511
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
add a comment |
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
1
1
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.
– Kaur Kadak
Nov 23 '18 at 14:09
add a comment |
You have to specify the range you want to look at.
len(a[1:2])
should do the trick.
len(a[x:y])
simply means "the length of a from element x to y (non-inclusive)"
So if you do len(a[2:2])
the output is 0.
add a comment |
You have to specify the range you want to look at.
len(a[1:2])
should do the trick.
len(a[x:y])
simply means "the length of a from element x to y (non-inclusive)"
So if you do len(a[2:2])
the output is 0.
add a comment |
You have to specify the range you want to look at.
len(a[1:2])
should do the trick.
len(a[x:y])
simply means "the length of a from element x to y (non-inclusive)"
So if you do len(a[2:2])
the output is 0.
You have to specify the range you want to look at.
len(a[1:2])
should do the trick.
len(a[x:y])
simply means "the length of a from element x to y (non-inclusive)"
So if you do len(a[2:2])
the output is 0.
answered Nov 23 '18 at 1:16
bunbun
2,03532446
2,03532446
add a comment |
add a comment |
You can use:
print(len(a[2:3]))
Or if want it builtin, do a function:
_len=len
def len(l):
if _len(l)==0:
return 0
elif isinstance(l[0],list):
return _len(l)
return _len([l])
a=[[1,1,1],[1,1,1],[1,1,1]]
print(len(a[2]))
Both Output:
1
add a comment |
You can use:
print(len(a[2:3]))
Or if want it builtin, do a function:
_len=len
def len(l):
if _len(l)==0:
return 0
elif isinstance(l[0],list):
return _len(l)
return _len([l])
a=[[1,1,1],[1,1,1],[1,1,1]]
print(len(a[2]))
Both Output:
1
add a comment |
You can use:
print(len(a[2:3]))
Or if want it builtin, do a function:
_len=len
def len(l):
if _len(l)==0:
return 0
elif isinstance(l[0],list):
return _len(l)
return _len([l])
a=[[1,1,1],[1,1,1],[1,1,1]]
print(len(a[2]))
Both Output:
1
You can use:
print(len(a[2:3]))
Or if want it builtin, do a function:
_len=len
def len(l):
if _len(l)==0:
return 0
elif isinstance(l[0],list):
return _len(l)
return _len([l])
a=[[1,1,1],[1,1,1],[1,1,1]]
print(len(a[2]))
Both Output:
1
answered Nov 23 '18 at 1:17
U9-Forward
13.1k21137
13.1k21137
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%2f53439591%2flength-of-a-single-sublist-in-python%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
How about
len(a[2:3])
?– Loocid
Nov 23 '18 at 1:13
len(a[2])
takes the length of the third element.len(a[0:2])
takes the number of elements in that range.– Cua
Nov 23 '18 at 2:07