Its a bit strange to know that i keep getting a KeyError exception only in one scenario. I tried zip on two string sequences and that object was then used inside the dict function. But when i try to index this sequence out i keep getting KeyError.
>>> rawdata = "acbd"
>>> l1 = "abcd"
>>> l2 = "cdef"
>>> zp1 = zip(l1, l2)
>>> type(zp1)
<class 'zip'>
>>> # zp1 is basically used for mapping. So every character in l1 is mapped to l2. So this would be
... # used to alter the rawdata object. Meaning every char in raw data would basically act as a key
... # to the below dictionary object and return a value which is then used in the join function.
...
>>> newstring = ''
>>> newstring = ''.join([dict(zp1)[x] for x in rawdata])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
KeyError: 'c'
The funny this is the same code works properly if i dont use the zp1 object rather i use zip(l1, l2) inside dict directly. Why does this happen? Any clues? Below is the code that works well.
>>> rawdata = "acbd"
>>> l1 = "abcd"
>>> l2 = "cdef"
>>> newstring = ''.join([dict(zip(l1, l2))[x] for x in rawdata])
>>> print(newstring) # Working as expected, without any exception
cedf