Removing items from a list while preserving order in python3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How can I remove items from a list but keep its original order?
Using remove()
seems to mess the order up.
Let's say for example a list like this:
['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
How can i remove the words "book" and "tree" without messing up the order?
python python-3.x list
add a comment |
How can I remove items from a list but keep its original order?
Using remove()
seems to mess the order up.
Let's say for example a list like this:
['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
How can i remove the words "book" and "tree" without messing up the order?
python python-3.x list
2
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
1
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12
add a comment |
How can I remove items from a list but keep its original order?
Using remove()
seems to mess the order up.
Let's say for example a list like this:
['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
How can i remove the words "book" and "tree" without messing up the order?
python python-3.x list
How can I remove items from a list but keep its original order?
Using remove()
seems to mess the order up.
Let's say for example a list like this:
['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
How can i remove the words "book" and "tree" without messing up the order?
python python-3.x list
python python-3.x list
edited Nov 15 '18 at 8:23
Patrick Artner
26.4k62544
26.4k62544
asked Nov 15 '18 at 7:50
AstudentAstudent
608
608
2
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
1
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12
add a comment |
2
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
1
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12
2
2
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
1
1
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12
add a comment |
2 Answers
2
active
oldest
votes
You are probably iterating your list while trying to figure out if you need to remove something - you never iterate a list that you want to insert/remove from - its a recipe for disaster.
Instead create a new list:
a = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
b = [e for e in a if e not in "book","tree"]
print(b)
Output:
['house', 'ambulance', 'window', 'Dragonball', 'alfa']
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
add a comment |
You could just use remove() since it doesn't change list order.
It is often best to just create a new object item like this:
item_list = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
item_list = [e for e in item_list if e not in ('book', 'alfa')]
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8: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%2f53314648%2fremoving-items-from-a-list-while-preserving-order-in-python3%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
You are probably iterating your list while trying to figure out if you need to remove something - you never iterate a list that you want to insert/remove from - its a recipe for disaster.
Instead create a new list:
a = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
b = [e for e in a if e not in "book","tree"]
print(b)
Output:
['house', 'ambulance', 'window', 'Dragonball', 'alfa']
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
add a comment |
You are probably iterating your list while trying to figure out if you need to remove something - you never iterate a list that you want to insert/remove from - its a recipe for disaster.
Instead create a new list:
a = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
b = [e for e in a if e not in "book","tree"]
print(b)
Output:
['house', 'ambulance', 'window', 'Dragonball', 'alfa']
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
add a comment |
You are probably iterating your list while trying to figure out if you need to remove something - you never iterate a list that you want to insert/remove from - its a recipe for disaster.
Instead create a new list:
a = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
b = [e for e in a if e not in "book","tree"]
print(b)
Output:
['house', 'ambulance', 'window', 'Dragonball', 'alfa']
You are probably iterating your list while trying to figure out if you need to remove something - you never iterate a list that you want to insert/remove from - its a recipe for disaster.
Instead create a new list:
a = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
b = [e for e in a if e not in "book","tree"]
print(b)
Output:
['house', 'ambulance', 'window', 'Dragonball', 'alfa']
answered Nov 15 '18 at 8:13
Patrick ArtnerPatrick Artner
26.4k62544
26.4k62544
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
add a comment |
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
This was it. If I had posted the full program I have, it might have been easier for experienced people to understand the problem. My bad, I tried to simplify the problem too much! Thank you!
– Astudent
Nov 15 '18 at 8:17
add a comment |
You could just use remove() since it doesn't change list order.
It is often best to just create a new object item like this:
item_list = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
item_list = [e for e in item_list if e not in ('book', 'alfa')]
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8:19
add a comment |
You could just use remove() since it doesn't change list order.
It is often best to just create a new object item like this:
item_list = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
item_list = [e for e in item_list if e not in ('book', 'alfa')]
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8:19
add a comment |
You could just use remove() since it doesn't change list order.
It is often best to just create a new object item like this:
item_list = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
item_list = [e for e in item_list if e not in ('book', 'alfa')]
You could just use remove() since it doesn't change list order.
It is often best to just create a new object item like this:
item_list = ['book', 'house', 'tree', 'ambulance', 'window', 'Dragonball', 'alfa']
item_list = [e for e in item_list if e not in ('book', 'alfa')]
answered Nov 15 '18 at 8:13
Stef van der ZonStef van der Zon
519211
519211
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8:19
add a comment |
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8:19
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8:19
This is the true. Sorry for the poorly edited question.
– Astudent
Nov 15 '18 at 8: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%2f53314648%2fremoving-items-from-a-list-while-preserving-order-in-python3%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
2
It totally doesn't.
– John Zwinck
Nov 15 '18 at 7:53
remove doesnt change the order
– Stef van der Zon
Nov 15 '18 at 8:08
1
do not iterate a list while removing stuff from it ... then you do not get messed up.
– Patrick Artner
Nov 15 '18 at 8:12