Delete duplicate tuples and lists in list












1














I have a list of tuples and lists in python:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


Where



[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]]


is the same as



[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]


So now, I want to remove these double elements, in order to have



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


For that, I want to use List comprehension and I've tried with



main_set = set(tuple(frozenset(innermost_list) for innermost_list in sublist) for sublist in gammagammalambda)


But I get the error:



TypeError: unhashable type: 'list'


Hope, you can help me.










share|improve this question


















  • 4




    How did you even end up with such a nested list structure?
    – SilverSlash
    Nov 21 '18 at 11:46










  • By using list.pop ;)
    – Armani42
    Nov 21 '18 at 11:57
















1














I have a list of tuples and lists in python:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


Where



[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]]


is the same as



[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]


So now, I want to remove these double elements, in order to have



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


For that, I want to use List comprehension and I've tried with



main_set = set(tuple(frozenset(innermost_list) for innermost_list in sublist) for sublist in gammagammalambda)


But I get the error:



TypeError: unhashable type: 'list'


Hope, you can help me.










share|improve this question


















  • 4




    How did you even end up with such a nested list structure?
    – SilverSlash
    Nov 21 '18 at 11:46










  • By using list.pop ;)
    – Armani42
    Nov 21 '18 at 11:57














1












1








1


1





I have a list of tuples and lists in python:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


Where



[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]]


is the same as



[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]


So now, I want to remove these double elements, in order to have



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


For that, I want to use List comprehension and I've tried with



main_set = set(tuple(frozenset(innermost_list) for innermost_list in sublist) for sublist in gammagammalambda)


But I get the error:



TypeError: unhashable type: 'list'


Hope, you can help me.










share|improve this question













I have a list of tuples and lists in python:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


Where



[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]]


is the same as



[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]


So now, I want to remove these double elements, in order to have



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


For that, I want to use List comprehension and I've tried with



main_set = set(tuple(frozenset(innermost_list) for innermost_list in sublist) for sublist in gammagammalambda)


But I get the error:



TypeError: unhashable type: 'list'


Hope, you can help me.







python list tuples list-comprehension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 11:43









Armani42

546




546








  • 4




    How did you even end up with such a nested list structure?
    – SilverSlash
    Nov 21 '18 at 11:46










  • By using list.pop ;)
    – Armani42
    Nov 21 '18 at 11:57














  • 4




    How did you even end up with such a nested list structure?
    – SilverSlash
    Nov 21 '18 at 11:46










  • By using list.pop ;)
    – Armani42
    Nov 21 '18 at 11:57








4




4




How did you even end up with such a nested list structure?
– SilverSlash
Nov 21 '18 at 11:46




How did you even end up with such a nested list structure?
– SilverSlash
Nov 21 '18 at 11:46












By using list.pop ;)
– Armani42
Nov 21 '18 at 11:57




By using list.pop ;)
– Armani42
Nov 21 '18 at 11:57












2 Answers
2






active

oldest

votes


















3














One alternative is the following:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

d = {frozenset(e[0]) : e for e in reversed(gammagammalambda)}
result = list(d.values())

print(result)


Output



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


Create a dictionary where the keys represent the values that must be unique, for each key set as value the entire element of gammagammalambda, finally the unique values are the values of the dictionary d.



Or a more straightforward alternative:



gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


seen = set()
result =
for e in gammagammalambda:
key = frozenset(e[0])
if key not in seen:
result.append(e)
seen.add(key)

print(result)


Output



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





share|improve this answer























  • Cool thank you very much. So where did you lern this stuff?
    – Armani42
    Nov 21 '18 at 11:54










  • @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
    – Daniel Mesejo
    Nov 21 '18 at 11:57










  • It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
    – jpp
    Nov 21 '18 at 12:11










  • The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
    – Armani42
    Nov 21 '18 at 12:14



















0














Similar to this answer, list is not hashable, tuple and frozenset are hashable.



You can accordingly define a "uniqueness key", and use the itertools unique_everseen recipe, also available in 3rd party libraries as toolz.unique or more_itertools.unique_everseen:



from more_itertools import unique_everseen

L = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

def unique_everseen(x):
return frozenset(x[0]), tuple(map(tuple, x[1][0]))

res = list(unique(L, key=unique_key))



[[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
[[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





share|improve this answer





















    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%2f53411318%2fdelete-duplicate-tuples-and-lists-in-list%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














    One alternative is the following:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

    d = {frozenset(e[0]) : e for e in reversed(gammagammalambda)}
    result = list(d.values())

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    Create a dictionary where the keys represent the values that must be unique, for each key set as value the entire element of gammagammalambda, finally the unique values are the values of the dictionary d.



    Or a more straightforward alternative:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    seen = set()
    result =
    for e in gammagammalambda:
    key = frozenset(e[0])
    if key not in seen:
    result.append(e)
    seen.add(key)

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





    share|improve this answer























    • Cool thank you very much. So where did you lern this stuff?
      – Armani42
      Nov 21 '18 at 11:54










    • @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
      – Daniel Mesejo
      Nov 21 '18 at 11:57










    • It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
      – jpp
      Nov 21 '18 at 12:11










    • The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
      – Armani42
      Nov 21 '18 at 12:14
















    3














    One alternative is the following:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

    d = {frozenset(e[0]) : e for e in reversed(gammagammalambda)}
    result = list(d.values())

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    Create a dictionary where the keys represent the values that must be unique, for each key set as value the entire element of gammagammalambda, finally the unique values are the values of the dictionary d.



    Or a more straightforward alternative:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    seen = set()
    result =
    for e in gammagammalambda:
    key = frozenset(e[0])
    if key not in seen:
    result.append(e)
    seen.add(key)

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





    share|improve this answer























    • Cool thank you very much. So where did you lern this stuff?
      – Armani42
      Nov 21 '18 at 11:54










    • @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
      – Daniel Mesejo
      Nov 21 '18 at 11:57










    • It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
      – jpp
      Nov 21 '18 at 12:11










    • The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
      – Armani42
      Nov 21 '18 at 12:14














    3












    3








    3






    One alternative is the following:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

    d = {frozenset(e[0]) : e for e in reversed(gammagammalambda)}
    result = list(d.values())

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    Create a dictionary where the keys represent the values that must be unique, for each key set as value the entire element of gammagammalambda, finally the unique values are the values of the dictionary d.



    Or a more straightforward alternative:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    seen = set()
    result =
    for e in gammagammalambda:
    key = frozenset(e[0])
    if key not in seen:
    result.append(e)
    seen.add(key)

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





    share|improve this answer














    One alternative is the following:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

    d = {frozenset(e[0]) : e for e in reversed(gammagammalambda)}
    result = list(d.values())

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    Create a dictionary where the keys represent the values that must be unique, for each key set as value the entire element of gammagammalambda, finally the unique values are the values of the dictionary d.



    Or a more straightforward alternative:



    gammagammalambda = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]


    seen = set()
    result =
    for e in gammagammalambda:
    key = frozenset(e[0])
    if key not in seen:
    result.append(e)
    seen.add(key)

    print(result)


    Output



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]], [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 11:54

























    answered Nov 21 '18 at 11:48









    Daniel Mesejo

    13.3k11027




    13.3k11027












    • Cool thank you very much. So where did you lern this stuff?
      – Armani42
      Nov 21 '18 at 11:54










    • @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
      – Daniel Mesejo
      Nov 21 '18 at 11:57










    • It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
      – jpp
      Nov 21 '18 at 12:11










    • The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
      – Armani42
      Nov 21 '18 at 12:14


















    • Cool thank you very much. So where did you lern this stuff?
      – Armani42
      Nov 21 '18 at 11:54










    • @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
      – Daniel Mesejo
      Nov 21 '18 at 11:57










    • It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
      – jpp
      Nov 21 '18 at 12:11










    • The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
      – Armani42
      Nov 21 '18 at 12:14
















    Cool thank you very much. So where did you lern this stuff?
    – Armani42
    Nov 21 '18 at 11:54




    Cool thank you very much. So where did you lern this stuff?
    – Armani42
    Nov 21 '18 at 11:54












    @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
    – Daniel Mesejo
    Nov 21 '18 at 11:57




    @Armani42 Most of that stuff I learned from the questions and answers here on stackoverflow. Also many PyCon talks and tutorials can be helpful.
    – Daniel Mesejo
    Nov 21 '18 at 11:57












    It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
    – jpp
    Nov 21 '18 at 12:11




    It seems the assumption is the ['q', 's'], ['t' ,'v'] bits need not be the same to ensure uniqueness. I'm not sure if this is the case, it could be the input data is not representative.
    – jpp
    Nov 21 '18 at 12:11












    The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
    – Armani42
    Nov 21 '18 at 12:14




    The thing is, I get these object by using a self written class. If you want to, I can send you the piece of the code somehow.
    – Armani42
    Nov 21 '18 at 12:14













    0














    Similar to this answer, list is not hashable, tuple and frozenset are hashable.



    You can accordingly define a "uniqueness key", and use the itertools unique_everseen recipe, also available in 3rd party libraries as toolz.unique or more_itertools.unique_everseen:



    from more_itertools import unique_everseen

    L = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

    def unique_everseen(x):
    return frozenset(x[0]), tuple(map(tuple, x[1][0]))

    res = list(unique(L, key=unique_key))



    [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
    [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





    share|improve this answer


























      0














      Similar to this answer, list is not hashable, tuple and frozenset are hashable.



      You can accordingly define a "uniqueness key", and use the itertools unique_everseen recipe, also available in 3rd party libraries as toolz.unique or more_itertools.unique_everseen:



      from more_itertools import unique_everseen

      L = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
      [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
      [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
      [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

      def unique_everseen(x):
      return frozenset(x[0]), tuple(map(tuple, x[1][0]))

      res = list(unique(L, key=unique_key))



      [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
      [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





      share|improve this answer
























        0












        0








        0






        Similar to this answer, list is not hashable, tuple and frozenset are hashable.



        You can accordingly define a "uniqueness key", and use the itertools unique_everseen recipe, also available in 3rd party libraries as toolz.unique or more_itertools.unique_everseen:



        from more_itertools import unique_everseen

        L = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

        def unique_everseen(x):
        return frozenset(x[0]), tuple(map(tuple, x[1][0]))

        res = list(unique(L, key=unique_key))



        [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]





        share|improve this answer












        Similar to this answer, list is not hashable, tuple and frozenset are hashable.



        You can accordingly define a "uniqueness key", and use the itertools unique_everseen recipe, also available in 3rd party libraries as toolz.unique or more_itertools.unique_everseen:



        from more_itertools import unique_everseen

        L = [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('r', 'u'), ('p', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('r', 'w'), ('p', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]

        def unique_everseen(x):
        return frozenset(x[0]), tuple(map(tuple, x[1][0]))

        res = list(unique(L, key=unique_key))



        [[[('p', 'u'), ('r', 'w')], [[, ['q', 's'], ['t', 'v'], ]]],
        [[('p', 'w'), ('r', 'u')], [[, ['q', 's'], ['t', 'v'], ]]]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 12:08









        jpp

        91.6k2052102




        91.6k2052102






























            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%2f53411318%2fdelete-duplicate-tuples-and-lists-in-list%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'