Removing items from a list while preserving order in python3












-1















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?










share|improve this question




















  • 2





    It totally doesn't.

    – John Zwinck
    Nov 15 '18 at 7:53











  • remove doesnt change the order

    – van der Zon Stef
    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
















-1















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?










share|improve this question




















  • 2





    It totally doesn't.

    – John Zwinck
    Nov 15 '18 at 7:53











  • remove doesnt change the order

    – van der Zon Stef
    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














-1












-1








-1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:23









Patrick Artner

23.1k62343




23.1k62343










asked Nov 15 '18 at 7:50









AstudentAstudent

538




538








  • 2





    It totally doesn't.

    – John Zwinck
    Nov 15 '18 at 7:53











  • remove doesnt change the order

    – van der Zon Stef
    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





    It totally doesn't.

    – John Zwinck
    Nov 15 '18 at 7:53











  • remove doesnt change the order

    – van der Zon Stef
    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

– van der Zon Stef
Nov 15 '18 at 8:08





remove doesnt change the order

– van der Zon Stef
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












2 Answers
2






active

oldest

votes


















3














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']





share|improve this answer
























  • 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





















3














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')]





share|improve this answer
























  • This is the true. Sorry for the poorly edited question.

    – Astudent
    Nov 15 '18 at 8:19











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
});


}
});














draft saved

draft discarded


















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









3














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']





share|improve this answer
























  • 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


















3














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']





share|improve this answer
























  • 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
















3












3








3







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']





share|improve this answer













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']






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 8:13









Patrick ArtnerPatrick Artner

23.1k62343




23.1k62343













  • 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







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















3














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')]





share|improve this answer
























  • This is the true. Sorry for the poorly edited question.

    – Astudent
    Nov 15 '18 at 8:19
















3














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')]





share|improve this answer
























  • This is the true. Sorry for the poorly edited question.

    – Astudent
    Nov 15 '18 at 8:19














3












3








3







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')]





share|improve this answer













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')]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 8:13









van der Zon Stefvan der Zon Stef

44110




44110













  • 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





This is the true. Sorry for the poorly edited question.

– Astudent
Nov 15 '18 at 8:19


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini