Reputation: 155
My Data set is mixed having lists and lists of lists. So I need to get unique objects from the lists or lists of lists but couldn't find any success. Input data set is as follows:
[[1, 2, 39], [1, 2, 39], [3], [[3], [4, 14, 63, 65], [66, 68, 82, 94]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [6, 91], [[7, 35, 60, 71], [73, 83, 95, 98]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [9, 72], [[10, 22, 30, 32], [38, 51, 56, 87], [89, 92]], [11], [12], [13, 90], [[4, 14, 63, 65], [66, 68, 82, 94]]]
Output Required:
[[1, 2, 39], [3], [4, 14, 63, 65], [66, 68, 82, 94], [5, 8, 31, 34], [36, 37, 42, 44], [55], [6, 91], [7, 35, 60, 71], [73, 83, 95, 98], [9, 72], [10, 22, 30, 32], [38, 51, 56, 87], [89, 92], [11], [12], [13, 90], [66, 68, 82, 94]]
abc=[]
for x in pool:
if x not in abc:
abc.append(x)
I am not getting that how can I choose distinct object of the list consisting of lists and lists of lists. Any help will be appreciated.
I have consulted multiple sources but nothing solves my problem. Few of them are as follows:
https://www.geeksforgeeks.org/python-get-unique-values-list/
Creating unique list of objects from multiple lists
Upvotes: 3
Views: 353
Reputation: 1346
If you have a flat list and you don't need to preserve order
def unique(input):
return list(set(input))
If preserving order is important you want an ordered dictionary.
Does Python have an ordered set?
Upvotes: 0
Reputation: 155
This Worked for me:
pool_list = []
#Remove Nested Pools
def reemovNestings(l):
for i in l:
#if type(i) == list:
if(True==any(isinstance(el, list) for el in i)):
reemovNestings(i)
else:
pool_list.append(i)
reemovNestings(pool)
print("Nested Pools Removed : \n" ,pool_list , "\n")
#Remove identical pools
unique = []
for i in pool_list:
if i not in unique:
unique.append(i)
print("Final Pool : \n ", unique)
Upvotes: 0
Reputation: 92440
You could flatten the list out with a generator then loop over the generated values and add to the result if it hasn't been seen. Something like:
def flatten(l):
for item in l:
if isinstance(item[0], list):
yield from flatten(item)
else:
yield item
seen = set()
unique = []
for l in flatten(l):
if tuple(l) in seen:
continue
seen.add(tuple(l))
unique.append(l)
print(unique)
Result
[[1, 2, 39],
[3],
[4, 14, 63, 65],
[66, 68, 82, 94],
[5, 8, 31, 34],
[36, 37, 42, 44],
[55],
[6, 91],
[7, 35, 60, 71],
[73, 83, 95, 98],
[9, 72],
[10, 22, 30, 32],
[38, 51, 56, 87],
[89, 92],
[11],
[12],
[13, 90]]
Upvotes: 2
Reputation: 43
You need to iterate each element in your data set to check if it is type = class "List". If so further element need to be checked. Here is the sample code you may refer
result = []
for list_item in list1:
for element in list_item:
if not isinstance(element, list) and list_item not in result:
result.append(list_item)
elif isinstance(element, list)and element not in result:
result.append(element)
Upvotes: 1
Reputation: 659
Lets say your list is set to a variable called my_list. You access any object from the first scope of this list with a normal index.
my_list = [[1, 2, 39], [1, 2, 39], [3], [[3], [4, 14, 63, 65], [66, 68, 82, 94]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [6, 91], [[7, 35, 60, 71], [73, 83, 95, 98]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [9, 72], [[10, 22, 30, 32], [38, 51, 56, 87], [89, 92]], [11], [12], [13, 90], [[4, 14, 63, 65], [66, 68, 82, 94]]] print(my_list[2])
This code above will print the 2nd index of my_list, which happens to be a list containg only the number 3
To access a list inside a list, specify the next index. At my_list[3]
this object holds 3 lists. [3], [4,14,63,65], and [66,68,82,94]
To choose the second list of these 3,
print(my_list[3][1])
first we choose one index [3]
which holds 3 lists then specify another index on that list [1]
which will give you [4,14,63,65]
To go another index deeper and retrieve an object from the list at my_list[3][1]
,add another index. 14 is at the [1] index of this list so to access it would be my_list[3][1][1]
Upvotes: 1