Is it possible to fix the size of a list in python so that if the list contains more than that it'll throw an errorFinding the index of an item given a list containing it in PythonHow do I parse a string to a float or int in Python?How do I determine the size of an object in Python?How do I sort a dictionary by value?How to split a string into a list?Split Strings into words with multiple word boundary delimitersHow to read a file line-by-line into a list?Create List of Single Item Repeated n Times in PythonWhy is reading lines from stdin much slower in C++ than Python?Get list from pandas DataFrame column headers
How much RAM could one put in a typical 80386 setup?
What is the offset in a seaplane's hull?
How do I create uniquely male characters?
How to re-create Edward Weson's Pepper No. 30?
If Manufacturer spice model and Datasheet give different values which should I use?
What is the command to reset a PC without deleting any files
Should I join office cleaning event for free?
A Journey Through Space and Time
Why can't I see bouncing of a switch on an oscilloscope?
Is it possible to make sharp wind that can cut stuff from afar?
Type 1 Error & Type 2 Error's pregnancy test analogy: is it legit?
The use of multiple foreign keys on same column in SQL Server
A function which translates a sentence to title-case
How can I fix this gap between bookcases I made?
Prevent a directory in /tmp from being deleted
Why is the design of haulage companies so “special”?
Is the language <p,n> belongs to NP class?
How to report a triplet of septets in NMR tabulation?
"which" command doesn't work / path of Safari?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
The magic money tree problem
How long does it take to type this?
Is it possible to do 50 km distance without any previous training?
If I cast Expeditious Retreat, can I Dash as a bonus action on the same turn?
Is it possible to fix the size of a list in python so that if the list contains more than that it'll throw an error
Finding the index of an item given a list containing it in PythonHow do I parse a string to a float or int in Python?How do I determine the size of an object in Python?How do I sort a dictionary by value?How to split a string into a list?Split Strings into words with multiple word boundary delimitersHow to read a file line-by-line into a list?Create List of Single Item Repeated n Times in PythonWhy is reading lines from stdin much slower in C++ than Python?Get list from pandas DataFrame column headers
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
n = int(input()) #fixed length
l = input().split()
the list that I want should be given as one line input so I dont know how range will help??
python python-3.x
add a comment |
n = int(input()) #fixed length
l = input().split()
the list that I want should be given as one line input so I dont know how range will help??
python python-3.x
1
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you by chance looking for themaxsplit
argument tosplit()
?
– Daniel Pryden
Mar 9 at 2:20
1
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27
add a comment |
n = int(input()) #fixed length
l = input().split()
the list that I want should be given as one line input so I dont know how range will help??
python python-3.x
n = int(input()) #fixed length
l = input().split()
the list that I want should be given as one line input so I dont know how range will help??
python python-3.x
python python-3.x
edited Mar 9 at 3:28
Magnus Melwin
77211122
77211122
asked Mar 9 at 2:06
LOWERCASELOWERCASE
81
81
1
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you by chance looking for themaxsplit
argument tosplit()
?
– Daniel Pryden
Mar 9 at 2:20
1
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27
add a comment |
1
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you by chance looking for themaxsplit
argument tosplit()
?
– Daniel Pryden
Mar 9 at 2:20
1
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27
1
1
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you by chance looking for the
maxsplit
argument to split()
?– Daniel Pryden
Mar 9 at 2:20
Are you by chance looking for the
maxsplit
argument to split()
?– Daniel Pryden
Mar 9 at 2:20
1
1
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27
add a comment |
2 Answers
2
active
oldest
votes
Check the length using len() and raise the error.
if len(l)>n:
raise Exception('Please give only inputs'.format(n))
add a comment |
You can use len to determine the size of a list or tuple.
if len(l) > n: # assuming n is the max size
print("List too large")
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%2f55073304%2fis-it-possible-to-fix-the-size-of-a-list-in-python-so-that-if-the-list-contains%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
Check the length using len() and raise the error.
if len(l)>n:
raise Exception('Please give only inputs'.format(n))
add a comment |
Check the length using len() and raise the error.
if len(l)>n:
raise Exception('Please give only inputs'.format(n))
add a comment |
Check the length using len() and raise the error.
if len(l)>n:
raise Exception('Please give only inputs'.format(n))
Check the length using len() and raise the error.
if len(l)>n:
raise Exception('Please give only inputs'.format(n))
answered Mar 9 at 2:18
sambasiva raosambasiva rao
1386
1386
add a comment |
add a comment |
You can use len to determine the size of a list or tuple.
if len(l) > n: # assuming n is the max size
print("List too large")
add a comment |
You can use len to determine the size of a list or tuple.
if len(l) > n: # assuming n is the max size
print("List too large")
add a comment |
You can use len to determine the size of a list or tuple.
if len(l) > n: # assuming n is the max size
print("List too large")
You can use len to determine the size of a list or tuple.
if len(l) > n: # assuming n is the max size
print("List too large")
answered Mar 9 at 2:15
whirishwhirish
303114
303114
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%2f55073304%2fis-it-possible-to-fix-the-size-of-a-list-in-python-so-that-if-the-list-contains%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
1
Are you editing the data at all? If not just use a tuple because it is immutable!
– Reedinationer
Mar 9 at 2:08
Are you by chance looking for the
maxsplit
argument tosplit()
?– Daniel Pryden
Mar 9 at 2:20
1
Sounds like an XY problem. What are you trying to solve with this?
– Klaus D.
Mar 9 at 2:27