Delete duplicate tuples and lists in list
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
add a comment |
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
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
add a comment |
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
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
python list tuples list-comprehension
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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'], ]]]]
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
add a comment |
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'], ]]]]
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%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
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'], ]]]]
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
add a comment |
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'], ]]]]
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
add a comment |
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'], ]]]]
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'], ]]]]
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
add a comment |
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
add a comment |
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'], ]]]]
add a comment |
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'], ]]]]
add a comment |
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'], ]]]]
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'], ]]]]
answered Nov 21 '18 at 12:08
jpp
91.6k2052102
91.6k2052102
add a comment |
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.
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.
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%2f53411318%2fdelete-duplicate-tuples-and-lists-in-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
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