I want to find and store the index of an element in a set of strings. Basically, I have a set of strings. If a new string lies in that set, I want it to be allotted the index number. If not, I want to add it to the set, and allot a new index number.
Now, I'm pretty sure I can do this using a dictionary with ease. I was wondering whether I could do the following, however:
s = set(['apple','banana','orange','mango','pineapple'])
if fruit in s:
print list(s).index(fruit)
else:
s.add(fruit)
print list(s).index(fruit)
Will the order of existing elements change if a new element is added, or a new instance of an existing element is added? I've tried it out and it doesn't seem to change, but I wanted to be sure.
Thanks.