I have been reading today about creating and using Sets. I have tried some sample code to see the results and I think it might be useful for me.

I am in search of a data type that will be able to store strings and\ or values culled from a program variable. I then will need to be able to search\ test this group of values for particular values using an IF statement (I think :confused:) If a particular, unique value already exists (which will be legal in my program) then the program will have to create a new, unique value (this will repeat until a unique number is within the Set, or rather, until a number registers as unique and is not repeated) This is simply something to test for repeated values in an unordered grouping.

I really wonder what you think: is a Python Set the best data type for this procedure? What about a tuple?

**Also, please note, I graduted 1-year ago from University where I studied Python but never used it much. This is not for homework, just practice projects preparing me a bit more for an upcoming job.


Since you initially allow duplicates and then want to change them you should use a list. A set would automatically eliminate a duplicate element, not what you want.

See, if you can use something like this ...

# go through each item in a list of strings and make each unique

list1 = list('abcdddeffgg')
print list1  # ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f', 'g', 'g']

n = 1
for index, item in enumerate(list1):
    count = list1.count(item)
    if count > 1:
        print index, item, count  # test
        item = item + str(n)
        list1[index] = item
        n += 1

print list1  # ['a', 'b', 'c', 'd1', 'd2', 'd', 'e', 'f3', 'f', 'g4', 'g']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.