Remove duplicates and finding unique sublists [duplicate] - python

This question already has answers here:
Get unique elements from list of lists when the order of the sublists does not matter
(2 answers)
Closed 9 years ago.
I have a nested list that looks like this:
lst = [[1,2,3],[1,2],[1,2,3],[2,3],[4,5],[2,3],[2,4],[4,2]]
I would like to find the unique sublists in lst. Using the above example, I'd like to find:
lst_set = [1,2,3],[1,2],[2,3],[4,5],[2,4]]
Order does not matter. In otherwords, [2,4] and [4,2] are the same.

set(tuple(sorted(i)) for i in lst)

In [22]: lst = [[1,2,3],[1,2],[1,2,3],[2,3],[4,5],[2,3],[2,4],[4,2]]
In [23]: set(frozenset(item) for item in lst)
Out[23]:
set([frozenset([2, 4]),
frozenset([1, 2]),
frozenset([2, 3]),
frozenset([1, 2, 3]),
frozenset([4, 5])])

Take a look at the built-in set() function. As the list is unhashable, you might need to turn the lists into a tuple before using set().
That means:
set([tuple(sorted(x)) for x in lst])

Related

How do I get the sum of individual lists within a larger list? [duplicate]

This question already has answers here:
How to apply a function to each sublist of a list in python?
(5 answers)
Closed 3 years ago.
So I know how to get the sum of a single list so say,
a=[1,2,3,4]
sum(a)
10
How would I go about trying to sum lists in a list of lists? So, from:
[[1,2],[2,3],[3,42]] to
[3,5,45]?
Code:
l = [[1,2],[2,3],[3,42]]
print([sum(i) for i in l])
Output:
[3, 5, 45]
You should use sum() in list comprehension for other lists
In [12]: a = [[1,2],[2,3],[3,42]]
In [13]: [sum(i) for i in a]
Out[13]: [3, 5, 45]
intial_list = [[1,2],[2,3],[3,42]]
res = list(map(sum, intial_list))
print(res)
output
[3,5,45]

Weird behaviour when iterating through list and deleting elements in python [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I'm trying to iterate over a Python list and delete each element once I've done some tasks with it, but it jumps one element after each iteration and I don't know why:
>>> simple_list = list(range(10))
>>> simple_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in simple_list:
... print(i)
... simple_list.remove(i)
...
0
2
4
6
8
>>> simple_list
[1, 3, 5, 7, 9]
Does anyone know why this is happening? Only the even elements are being removed, and looks like the loop doesn't go through the uneven ones.
Well, your list is shrinking while you are iterating over it. If you want to, just look at the first element while your iterate over it.
while len(simple_list) > 0:
print(simple_list[0])
del simple_list[0]
You can use list comprehension to get copy of the array and then iterate.
simple_list = list(range(10))
for i in simple_list[:]:
print(i)
simple_list.remove(i)
Or this:
for i in simple_list[:]:
simple_list.remove(i)
print(simple_list)
Output:
[]
Ok, found the answer here: Python: Removing list element while iterating over list
You should NEVER delete an element from a list while iterating over it in a for loop. You could use a while loop instead. Or, record the indices of all the elements you want to remove and then delete them after the iteration is complete

python remove elements from list based on other list [duplicate]

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 6 years ago.
I'm stuck with a part in one of my codes where I have to delete all the occurances present in listA that are identical in listB.
Example:
A=[1,4,4,4,3,3,2,1,5,5]
B=[4,3]
Result should be A=[1,2,1,5,5]. Ideally I would want to do it in linear time.
using Set Operations:
list(set(A) - set(B))
Using List Comprehension
list(set([i for i in A if i not in B]))
Try with list comprehension,
In [11]: [i for i in A if i not in B]
Out[11]: [1, 2, 1, 5, 5]

append values in a list without sorting python, keeping the appending order [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 7 years ago.
I am producing a list of list by this code, it is just a condition for a column of a dataframe named lvl or "Level", then append the index of this condition values, so the problem that i got is that the order of appending is important to me,
for i in range(1,int(24-lvl)):
j=list2[(list2.lvl==(lvl+i))]
jj=[]
jj.append(j.index)
print itertools.chain(jj)
well for example, the answer should be:
[0,100,110,500,501,550,555,89,120,114]
but i get the same list but sorted
[0,89,100,110,114,120,500,501,550,555]
itertools.chain works for me. You need to unpack the list before passing it to chain method.
>>> l = [[1,5],[10,2],[6,9,3]]
>>> list(itertools.chain(*l))
[1, 5, 10, 2, 6, 9, 3]
You can simply do it with list comprehension:
>>> l = [[1,5],[10,2],[6,9,3]]
>>> l_out = [item for sub_l in l for item in sub_l]
>>> l_out
[1, 5, 10, 2, 6, 9, 3]

Python eliminate duplicates of list with unhashable elements in one line [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: removing duplicates from a list of lists
Say i have list
a=[1,2,1,2,1,3]
If all elements in a are hashable (like in that case), this would do the job:
list(set(a))
But, what if
a=[[1,2],[1,2],[1,3]]
?
Python 2
>>> from itertools import groupby
>>> a = [[1,2],[1,2],[1,3]]
>>> [k for k,v in groupby(sorted(a))]
[[1, 2], [1, 3]]
Works also in Python 3 but with caveat that all elements must be orderable types.
This set comprehension works for the List of Lists to produce a set of tuples:
>>> {(tuple(e)) for e in a}
set([(1, 2), (1, 3)])
Then use that to turn it into a list of lists again with no duplicates:
>>> [list(x) for x in {(tuple(e)) for e in a}]
[[1, 2], [1, 3]]

Categories