How to append into a list if it doesn't find anything that matches the list?
I want to make a print when my code doesn't find anything throughout the whole list.
This is my code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
condition = True
while condition:
try:
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
condition = False
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_lis
condition = False
else:
old_list.append(new_list)
condition = False
break
except Exception as err:
print(err)
What I want do is: check every first word of each % and then I make a compare from old_list to check if there is the first word inside the old_list - if there is one then we replay it the old_list value with the new one.
elif if the word is longer than then the new_list word then we dont do anything.
How can I make so that if it doesn't find if a[0] in items: at the whole list then we add the new_list into old_list bascially?
Meaning it needs to first search for all words if it contains inside the old_list and if it is not then we append.
New edit:
Code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
break
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_list
break
else:
old_list.append(new_list)
break
python for-loop while-loop
add a comment |
I want to make a print when my code doesn't find anything throughout the whole list.
This is my code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
condition = True
while condition:
try:
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
condition = False
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_lis
condition = False
else:
old_list.append(new_list)
condition = False
break
except Exception as err:
print(err)
What I want do is: check every first word of each % and then I make a compare from old_list to check if there is the first word inside the old_list - if there is one then we replay it the old_list value with the new one.
elif if the word is longer than then the new_list word then we dont do anything.
How can I make so that if it doesn't find if a[0] in items: at the whole list then we add the new_list into old_list bascially?
Meaning it needs to first search for all words if it contains inside the old_list and if it is not then we append.
New edit:
Code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
break
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_list
break
else:
old_list.append(new_list)
break
python for-loop while-loop
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25
add a comment |
I want to make a print when my code doesn't find anything throughout the whole list.
This is my code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
condition = True
while condition:
try:
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
condition = False
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_lis
condition = False
else:
old_list.append(new_list)
condition = False
break
except Exception as err:
print(err)
What I want do is: check every first word of each % and then I make a compare from old_list to check if there is the first word inside the old_list - if there is one then we replay it the old_list value with the new one.
elif if the word is longer than then the new_list word then we dont do anything.
How can I make so that if it doesn't find if a[0] in items: at the whole list then we add the new_list into old_list bascially?
Meaning it needs to first search for all words if it contains inside the old_list and if it is not then we append.
New edit:
Code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
break
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_list
break
else:
old_list.append(new_list)
break
python for-loop while-loop
I want to make a print when my code doesn't find anything throughout the whole list.
This is my code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
condition = True
while condition:
try:
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
condition = False
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_lis
condition = False
else:
old_list.append(new_list)
condition = False
break
except Exception as err:
print(err)
What I want do is: check every first word of each % and then I make a compare from old_list to check if there is the first word inside the old_list - if there is one then we replay it the old_list value with the new one.
elif if the word is longer than then the new_list word then we dont do anything.
How can I make so that if it doesn't find if a[0] in items: at the whole list then we add the new_list into old_list bascially?
Meaning it needs to first search for all words if it contains inside the old_list and if it is not then we append.
New edit:
Code:
new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
for i, items in zip(range(len(old_list)), old_list):
old_list_value = old_list[i].split("%")
if a[0] in items:
if len(old_list_value[1]) < len(a[1]):
old_list[i] = new_list
break
elif len(old_list_value[1]) > len(a[1]):
old_list[i] = new_list
break
else:
old_list.append(new_list)
break
python for-loop while-loop
python for-loop while-loop
edited Nov 21 '18 at 23:23
Hellosiroverthere
asked Nov 21 '18 at 23:12
HellosiroverthereHellosiroverthere
10818
10818
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25
add a comment |
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25
add a comment |
1 Answer
1
active
oldest
votes
You might want to check out the for-else:
for item in items:
if you_found_something:
found_item = item
break
else:
print('No item found.')
do_your_append_here
Basically the for-else answers the question if the for loop terminated without a break. Above we break when we find something therefore the else won't be executed but as far as your for loop terminates without a break statement, then the code in else will be executed.
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
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%2f53421747%2fhow-to-append-into-a-list-if-it-doesnt-find-anything-that-matches-the-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might want to check out the for-else:
for item in items:
if you_found_something:
found_item = item
break
else:
print('No item found.')
do_your_append_here
Basically the for-else answers the question if the for loop terminated without a break. Above we break when we find something therefore the else won't be executed but as far as your for loop terminates without a break statement, then the code in else will be executed.
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
add a comment |
You might want to check out the for-else:
for item in items:
if you_found_something:
found_item = item
break
else:
print('No item found.')
do_your_append_here
Basically the for-else answers the question if the for loop terminated without a break. Above we break when we find something therefore the else won't be executed but as far as your for loop terminates without a break statement, then the code in else will be executed.
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
add a comment |
You might want to check out the for-else:
for item in items:
if you_found_something:
found_item = item
break
else:
print('No item found.')
do_your_append_here
Basically the for-else answers the question if the for loop terminated without a break. Above we break when we find something therefore the else won't be executed but as far as your for loop terminates without a break statement, then the code in else will be executed.
You might want to check out the for-else:
for item in items:
if you_found_something:
found_item = item
break
else:
print('No item found.')
do_your_append_here
Basically the for-else answers the question if the for loop terminated without a break. Above we break when we find something therefore the else won't be executed but as far as your for loop terminates without a break statement, then the code in else will be executed.
edited Nov 21 '18 at 23:19
answered Nov 21 '18 at 23:16
RafaelRafael
2,88932130
2,88932130
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
add a comment |
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
In that case I dont need to actually do a while loop and stuff that I did?
– Hellosiroverthere
Nov 21 '18 at 23:18
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
That's correct. You might want to restructure your solution with this new syntax in mind, which should solve your problem.
– Rafael
Nov 21 '18 at 23:19
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%2f53421747%2fhow-to-append-into-a-list-if-it-doesnt-find-anything-that-matches-the-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
You don't need a break statement in the else.
– Rafael
Nov 21 '18 at 23:25