Uniquifying a list of lists in python - python

Up until now I have been using this code to uniquify (remove duplicates) from list in python:
my_list = list(set(my_list))
I now have a list of lists, I want to be able to remove duplicates from within the list of lists. For example:
(['possible-duplicate', 'random-data'], ['possible-duplicate', 'random-data'], ['possible-duplicate', 'random-data'])
I want to remove the whole sublist if possible-duplicate is a duplicate.
Can this be done?
Thanks

seen = set()
[sublist for sublist in my_list if sublist[0] not in seen and not seen.add(sublist[0])]
This happens to preserve order as well, which list(set(...)) does not.

Make a dictionary from your data:
data = (['possible-duplicate', '12345'],
['not-a-duplicate', '54321'],
['possible-duplicate', '51423'])
data_unique = dict(data)
Result is {'not-a-duplicate': '54321', 'possible-duplicate': '51423'}, or if you prefer a list of tuples, use date_unique.items(), which gives you [('not-a-duplicate', '54321'), ('possible-duplicate', '51423')].
Or for the more general case, where the sublists have more than two elements, you can use this
data_unique = dict((d[0], d) for d in data)
and then use data_unique.values() to reclaim the "uniquified" list.

Related

How to convert the list into a list of dictionaries?

I have a list like this.
ls = ['Size:10,color:red,', 'Size:10,color: blue,']
I want to convert the list into this format.
[{'Size':'10','color':'red'}, {'Size':'10','color': 'blue'}]
What I have tried is:
[dict([pair.split(":", 1)]) for pair in ls]
# It gave me output like this.
[{'Size': '10,color:red,'}, {'Size': '10,color: blue,'}]
But this method works if the list is like this ['color:blue,'] but didn't worked properly with the above list.
We can see that for pair in ls in your list comprehension is already doubtful, because elements of ls are not pairs. Each element actually contains a sequence of pairs.
There will be two loops needed here, one to iterate the outer list, and then another one to iterate within each value, since those values are actually strings consisting of multiple fields.
While this is possible with a nested list comprehension, it will be easier (and more readable) if you break the problem down into simpler parts rather than trying to fit it all into one-line.
result = []
for text in ls:
d = {}
pairs = text.strip(",").split(",")
for pair in pairs:
key, val = pair.split(":")
d[key] = val.strip()
result.append(d)

How to add up values of the "sublists" within a list of lists

I have a list of lists in my script:
list = [[1,2]
[4,3]
[6,2]
[1,6]
[9,2]
[6,5]]
I am looking for a solution to sum up the contents of each "sublist" within the list of lists. The desired output would be:
new_list = [3,7,8,7,11,11]
I know about combining ALL of these lists into one which would be:
new_list = [27,20]
But that's not what i'm looking to accomplish.
I need to combine the two values within these "sublists" and have them remain as their own entry in the main list.
I would also greatly appreciate it if it was also explained how you solved the problem rather than just handing me the solution. I'm trying to learn python so even a minor explanation would be greatly appreciated.
Using Python 3.7.4
Thanks, Riftie.
The "manual" solution will be using a for loop.
new_list = []
for sub_list in list:
new_list.append(sum(sub_list))
or as list compherension:
new_list = [sum(sub_list) for sub_list in list]
The for loop iterates through the elements of list. In your case, list is a list of lists. So every element is a list byitself. That means that while iterating, sub_list is a simple list. To get a sum of list I used sum() build-in function. You can of course iterate manually and sum every element:
new_list = []
for sub_list in list:
sum_val = 0
for element in sub_list:
sum_val = sum_val + element
new_list.append(sum_val)
but no need for that.
A better approach will be to use numpy, which allows you to sum by axis, as it looks on list of lists like an array. Since you are learning basic python, it's too soon to learn about numpy. Just keep in mind that there is a package for handling multi-dimensions arrays and it allows it perform some actions like sum on an axis by your choice.
Edit: I've seen the other solution suggest. As both will work, I believe this solution is more "accessible" for someone who learn to program for first time. Using list comprehension is great and correct, but may be a bit confusing while first learning. Also as suggested, calling your variables list is a bad idea because it's keyword. Better names will be "my_list", "tmp_list" or something else.
Use list comprehension. Also avoid using keywords as variable names, in your case you overrode the builtin list.
# a, b -> sequence unpacking
summed = [a + b for a, b in lst] # where lst is your list of lists
# if the inner lists contain variable number of elements, a more
# concise solution would be
summed2 = [sum(seq) for seq in lst]
Read more about the powerful list comprehension here.

how can i add a empty list to each element of list

I have list like mylist=['a','b','c',1,2,3]. iterating through each element of list, I wanna update this list as mylist=[['a',[]],['b',[]],['c',[]],[1,[]],[2,[]],[3,[]]]. the important thing is that each list associated with elements of mylist should not point to the same list. i.e., mylist[0][1] and mylist[1][1] should not point to the same list. So that i can modify each sub list independently. Hope my question is clear. Thanks in advance
This can be achieved easily using a list comprehension along your original myList. By adding an empty list on each iteration, there is no problem with sharing a reference to the same list, which you seem to be concerned about.
myList = [[element, []] for element in myList]

insert item from an ordered list to a tuple according to index

mylist=[[('The','d'),('apple','n'),('is','v'),('red','a')],[('I','p'),('feel','v'),('fine','adv')]]
This is a list of lists of tuples, and I wish to create a new list of tuples with information from another list added to it accordingly.
new_list=[['b','i','o','o'],['o','o','o']]
for each sub_list of these two lists, I have the exact same number of items as illustrated, and I wish to add the first string of the first list in new_list to the first tuple of the first list in my_list, the second string of the first list in new_list to second tuple of first list in my_list, and so on.
The ideal output looks like this
output=[[('The','d','b'),('apple','n','i'),('is','v','o'),('red','a','o')],[('I','p','o'),('feel','v','o'),('fine','adv','o')]]
I'd appreciate any suggestions, thank you!!
Basically this question has been answered elsewhere, so I flagged the question as duplicate, but assuming that you are new to python, here is a solution to your problem.
output = [[(*t,c) for t,c in zip(l1,l2)] for l1,l2 in zip(mylist,new_list)]
The new output is created using a list nested list comprehension (creating a list of lists). To match corresponding items from two lists together, you can use zip, which returns a generator of tuples of these corresponding items. The for loop that iterates through these tuples unpacks them into two separate items, i.e.
for l1,l2 in zip(mylist,newlist)
groups the sublists of mylist and newlist together, which the for are then unpacked into the lists l1 and l2 during the for iteration. Finally, as the items of the sublists of my_list are tuples, I unpack these tuples 'on the fly' while generating the new tuple: (*t,c) is the same as (t[0],t[1],c).
Please ask if anything stayed unclear.
Hope this helps.

Outputting dictionary results for all indexes

I'm trying to output some keys, for all indexes, e.g:
print results["result"][0]["name"]
prints out the first key with no problems, and so does [1], [2], etc. I want to be able to print all indexes, for the value "name". I'm sure it uses a for loop, and tried several methods, but have failed.
How can I print the results for all indexes, not just 1?
Assuming results['result'] is a list, use a for loop to iterate over the items in that list:
for item in results['result']:
print(item['name'])
So assuming results is a dictionary-like object, results["result"] is a list containing dictionary-like elements, where these elements have a key "name",
You could use a list comprehension:
print([e["name"] for e in results["result"])
If results["result"] is a dict, this would be:
print([e["name"] for e in results["result"].values())
# or, less efficiently
print([results["result"][e]["name"] for e in results["result"])
Just join the list comprehension with newlines:
print '\n'.join(i["name"] for i in results["result"])

Categories

Resources