I\'ve got a string with words that are separated by spaces (all words are unique, no duplicates). I turn this string into list:
s = \"#one cat #two dogs #th
The problem is whenever you delete a value from the list, that particular list restores its values dynamically.
That is, when you perform out.remove(ind)
and out.remove(ind+1)
, the values in these indexes are deleted,
but they are replaced with new values which are predecessor of the previous value.
Therefore to avoid this you have to implement the code as follows :
out = []
out = '#one cat #two dogs #three birds'.split()
print "The list is : {0} \n".format(out)
myDictionary = dict()
for x in out:
if '#' in x:
ind = out.index(x) # Get current index
nextValue = out[ind+1] # Get next value
myDictionary[x] = nextValue
out = [] # #emptying the list
print("The dictionary is : {0} \n".format(myDictionary))
So, after you are done transferring the values from the list to dictionary, we could safely empty the out
by
using out = []