How to remove the inner units in a compound list in Python?












-6














x = [['a','b'],['c','d','g'],['e','f','h','i','j'].......['zzy','xxx']]


If I got a compound list like this (a large list) in Python, how can I elegantly remove only, say, the element 'c' without removing the whole element ['c','d','g'] together?
Obviously merely list.remove() doesn't work for this, and implementing a for loop works



for i in x:
for j in i:
if j == 'c':
i = i.remove(j)


but is computationally expensive since it's a very long list...



thank you










share|improve this question




















  • 3




    What have you tried so far?
    – G. Anderson
    Nov 20 at 21:34






  • 5




    your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
    – d_kennetz
    Nov 20 at 21:36










  • Is the list always 2-d or can it be arbitrarily deep?
    – slider
    Nov 20 at 21:38










  • My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
    – D.Chain
    Nov 20 at 21:56
















-6














x = [['a','b'],['c','d','g'],['e','f','h','i','j'].......['zzy','xxx']]


If I got a compound list like this (a large list) in Python, how can I elegantly remove only, say, the element 'c' without removing the whole element ['c','d','g'] together?
Obviously merely list.remove() doesn't work for this, and implementing a for loop works



for i in x:
for j in i:
if j == 'c':
i = i.remove(j)


but is computationally expensive since it's a very long list...



thank you










share|improve this question




















  • 3




    What have you tried so far?
    – G. Anderson
    Nov 20 at 21:34






  • 5




    your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
    – d_kennetz
    Nov 20 at 21:36










  • Is the list always 2-d or can it be arbitrarily deep?
    – slider
    Nov 20 at 21:38










  • My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
    – D.Chain
    Nov 20 at 21:56














-6












-6








-6


0





x = [['a','b'],['c','d','g'],['e','f','h','i','j'].......['zzy','xxx']]


If I got a compound list like this (a large list) in Python, how can I elegantly remove only, say, the element 'c' without removing the whole element ['c','d','g'] together?
Obviously merely list.remove() doesn't work for this, and implementing a for loop works



for i in x:
for j in i:
if j == 'c':
i = i.remove(j)


but is computationally expensive since it's a very long list...



thank you










share|improve this question















x = [['a','b'],['c','d','g'],['e','f','h','i','j'].......['zzy','xxx']]


If I got a compound list like this (a large list) in Python, how can I elegantly remove only, say, the element 'c' without removing the whole element ['c','d','g'] together?
Obviously merely list.remove() doesn't work for this, and implementing a for loop works



for i in x:
for j in i:
if j == 'c':
i = i.remove(j)


but is computationally expensive since it's a very long list...



thank you







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:15

























asked Nov 20 at 21:34









D.Chain

33




33








  • 3




    What have you tried so far?
    – G. Anderson
    Nov 20 at 21:34






  • 5




    your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
    – d_kennetz
    Nov 20 at 21:36










  • Is the list always 2-d or can it be arbitrarily deep?
    – slider
    Nov 20 at 21:38










  • My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
    – D.Chain
    Nov 20 at 21:56














  • 3




    What have you tried so far?
    – G. Anderson
    Nov 20 at 21:34






  • 5




    your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
    – d_kennetz
    Nov 20 at 21:36










  • Is the list always 2-d or can it be arbitrarily deep?
    – slider
    Nov 20 at 21:38










  • My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
    – D.Chain
    Nov 20 at 21:56








3




3




What have you tried so far?
– G. Anderson
Nov 20 at 21:34




What have you tried so far?
– G. Anderson
Nov 20 at 21:34




5




5




your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
– d_kennetz
Nov 20 at 21:36




your question leaves a lot to be interpreted by us. Do you want this to be applicable only to this small list? why is 'a' a smaller unit than 'b'? 'a' and 'b' are strings. You also didn't provide anything you have tried so far.
– d_kennetz
Nov 20 at 21:36












Is the list always 2-d or can it be arbitrarily deep?
– slider
Nov 20 at 21:38




Is the list always 2-d or can it be arbitrarily deep?
– slider
Nov 20 at 21:38












My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
– D.Chain
Nov 20 at 21:56




My apology. I'm new to Python and am not fully used to asking here. I've edited it a little and if you could have some look i'd appreciate it.
– D.Chain
Nov 20 at 21:56












2 Answers
2






active

oldest

votes


















0














The only improvement I could find to your code is:



x = [['a','b'],['c','d','g'],['e','f','h','i','j'],['zzy','xxx']]

def remove(compound_list, elem):
for lst in compound_list:
for ix, item in enumerate(lst):
if item == elem:
del lst[ix]
remove(x, 'c')


It is slow O(n^2) but it is faster that first solution which counting i.remove(j) it's O(n^3)






share|improve this answer





























    0














    You can find the "smallest" element using min (provide a custom key function if you want to change the definition of smallest).



    An O(n) solution (where n is the total number of elements in all the lists) can be to rebuild each list by omitting the smallest element:



    x = [['a','b'],['b','b'],['c','c']]

    smallest = min(min(l) for l in x)

    x = [[e for e in l if e != smallest] for l in x]
    print(x)
    # [['b'], ['b', 'b'], ['c', 'c']]





    share|improve this answer























    • thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
      – D.Chain
      Nov 20 at 21:58












    • @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
      – slider
      Nov 20 at 22:03











    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%2f53401916%2fhow-to-remove-the-inner-units-in-a-compound-list-in-python%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









    0














    The only improvement I could find to your code is:



    x = [['a','b'],['c','d','g'],['e','f','h','i','j'],['zzy','xxx']]

    def remove(compound_list, elem):
    for lst in compound_list:
    for ix, item in enumerate(lst):
    if item == elem:
    del lst[ix]
    remove(x, 'c')


    It is slow O(n^2) but it is faster that first solution which counting i.remove(j) it's O(n^3)






    share|improve this answer


























      0














      The only improvement I could find to your code is:



      x = [['a','b'],['c','d','g'],['e','f','h','i','j'],['zzy','xxx']]

      def remove(compound_list, elem):
      for lst in compound_list:
      for ix, item in enumerate(lst):
      if item == elem:
      del lst[ix]
      remove(x, 'c')


      It is slow O(n^2) but it is faster that first solution which counting i.remove(j) it's O(n^3)






      share|improve this answer
























        0












        0








        0






        The only improvement I could find to your code is:



        x = [['a','b'],['c','d','g'],['e','f','h','i','j'],['zzy','xxx']]

        def remove(compound_list, elem):
        for lst in compound_list:
        for ix, item in enumerate(lst):
        if item == elem:
        del lst[ix]
        remove(x, 'c')


        It is slow O(n^2) but it is faster that first solution which counting i.remove(j) it's O(n^3)






        share|improve this answer












        The only improvement I could find to your code is:



        x = [['a','b'],['c','d','g'],['e','f','h','i','j'],['zzy','xxx']]

        def remove(compound_list, elem):
        for lst in compound_list:
        for ix, item in enumerate(lst):
        if item == elem:
        del lst[ix]
        remove(x, 'c')


        It is slow O(n^2) but it is faster that first solution which counting i.remove(j) it's O(n^3)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 1:01









        edilio

        69656




        69656

























            0














            You can find the "smallest" element using min (provide a custom key function if you want to change the definition of smallest).



            An O(n) solution (where n is the total number of elements in all the lists) can be to rebuild each list by omitting the smallest element:



            x = [['a','b'],['b','b'],['c','c']]

            smallest = min(min(l) for l in x)

            x = [[e for e in l if e != smallest] for l in x]
            print(x)
            # [['b'], ['b', 'b'], ['c', 'c']]





            share|improve this answer























            • thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
              – D.Chain
              Nov 20 at 21:58












            • @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
              – slider
              Nov 20 at 22:03
















            0














            You can find the "smallest" element using min (provide a custom key function if you want to change the definition of smallest).



            An O(n) solution (where n is the total number of elements in all the lists) can be to rebuild each list by omitting the smallest element:



            x = [['a','b'],['b','b'],['c','c']]

            smallest = min(min(l) for l in x)

            x = [[e for e in l if e != smallest] for l in x]
            print(x)
            # [['b'], ['b', 'b'], ['c', 'c']]





            share|improve this answer























            • thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
              – D.Chain
              Nov 20 at 21:58












            • @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
              – slider
              Nov 20 at 22:03














            0












            0








            0






            You can find the "smallest" element using min (provide a custom key function if you want to change the definition of smallest).



            An O(n) solution (where n is the total number of elements in all the lists) can be to rebuild each list by omitting the smallest element:



            x = [['a','b'],['b','b'],['c','c']]

            smallest = min(min(l) for l in x)

            x = [[e for e in l if e != smallest] for l in x]
            print(x)
            # [['b'], ['b', 'b'], ['c', 'c']]





            share|improve this answer














            You can find the "smallest" element using min (provide a custom key function if you want to change the definition of smallest).



            An O(n) solution (where n is the total number of elements in all the lists) can be to rebuild each list by omitting the smallest element:



            x = [['a','b'],['b','b'],['c','c']]

            smallest = min(min(l) for l in x)

            x = [[e for e in l if e != smallest] for l in x]
            print(x)
            # [['b'], ['b', 'b'], ['c', 'c']]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 at 22:02

























            answered Nov 20 at 21:50









            slider

            8,0151129




            8,0151129












            • thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
              – D.Chain
              Nov 20 at 21:58












            • @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
              – slider
              Nov 20 at 22:03


















            • thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
              – D.Chain
              Nov 20 at 21:58












            • @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
              – slider
              Nov 20 at 22:03
















            thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
            – D.Chain
            Nov 20 at 21:58






            thanks a lot for your answer! but it's actually a very long list so using loop may cost much time..i've edited the question to make it clearer
            – D.Chain
            Nov 20 at 21:58














            @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
            – slider
            Nov 20 at 22:03




            @D.Chain I've updated the answer to not use remove since, as you rightly pointed out, it is computationally expensive to call it on each list. A computationally (in terms of time) more efficient alternative can be to rebuild the lists.
            – slider
            Nov 20 at 22:03


















            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.





            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401916%2fhow-to-remove-the-inner-units-in-a-compound-list-in-python%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







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)