Python Forum
Making lists using itertools and eliminating duplicates. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Making lists using itertools and eliminating duplicates. (/thread-30572.html)



Making lists using itertools and eliminating duplicates. - mike3891 - Oct-26-2020

I'm trying to get all combinations of the following numbers as long as they don't total over 50,000 and must be in combos of 6. From there I want to condense into a new list that has no duplicates even if they're are out of order (ex: [8000, 9300, 7000, 8800, 7400, 9200] would be eliminated if [8800, 7400, 9200, 8000, 9300, 7000] was already in the list).

I think my code is correct but it is taking forever for the ouput to load in PyCharm. For now, I'm just looking to see if the len of the lists will be different to see if it is worth eliminating duplicates.

Sorry but I don't know how to get it to hold the indents when I post.

import itertools

num = {
9200, 8200, 7600, 9100, 7400, 8900, 7900,
7500, 7700, 6800, 9300, 9000, 7000, 8000,
8600, 7100, 8800, 7300, 8300, 8700, 8500,
9400, 6900, 7200
}

entry = []
for i in num:
for combination in itertools.combinations(num, 6):
if sum(combination) <= 50000:
entry.append(combination)

non_dup = []
for i in entry:
if i not in non_dup:
non_dup.append(i)



print("Entry list: " + str(len(entry)))
print("Non-Dup list: " + str(len(non_dup)))


RE: Making lists using itertools and eliminating duplicates. - mike3891 - Oct-26-2020

Figured it out. Sorry, I don't know how to delete a post.

print(len(list(set(entry))))


RE: Making lists using itertools and eliminating duplicates. - bowlofred - Oct-26-2020

You use bbcode "python" tags around your code in the post to display properly. You can click the Python logo button above the editor to insert them automatically.

If you just need the size of the set, you don't have to convert it to a list. len() works just fine on sets, so

print(len(set(entry)))
should be sufficient. If you don't need the ordering, you could just create non_dup as a set in the first place and then add objects to it (rather than append them).