Say I create a dictionary like so:
self.defaultValues = {}
self.defaultValues["A"] = "10"
self.defaultValues["B"] = "100"
self.defaultValues["C"] = "20"
self.defaultValues["D"] = "12.5"
I want to be able to iterate the items in the same order I added them, meaning
for k in self.defaultValues:
print k
would result in
A
B
C
D
Also, if I have another list, not necessarily of the same length. I want to iterate both the dictionary and the list, and if a value exist in the list I will print it, otherwise I will print if form the dictionary. Its easy to do with a simple
if self.list.count(value) != 0:
But I think there might be a more elegant way to do it.
Thanks!
For the first problem, use an
OrderedDict:OrderedDictrespects the order in which elements were inserted.For your second question (you should really make separate questions for separate problems), you can use
into test if an object is in the list: