Issue
I have a list (colNames) and I am trying to read each key in the dictionary and iterate thru the list to find the corresponding index wherein the key matches the item in the list
colNames = ['First_Name', 'Last_Name', 'ClientID', 'Location']
dict = {'First_Name':'Charlie', 'Last_Name':'Brown', 'ClientID':'23156', 'Location':'H3M 2V4'}
for key, value in dict.iteritems():
[item for item in colNames if key==item]
col = colNames.index(item)
print item
The result is always 'Location' irrespective of the key
Why is that? I assumed it would iterate thru the list and once it finds a match with the key note the item and using colNames.index(item) i could get the index.
Solution
This should do what you want:
colNames = ['First_Name', 'Last_Name', 'ClientID', 'Location']
_dict = {'First_Name':'Charlie', 'Last_Name':'Brown', 'ClientID':'23156', 'Location':'H3M 2V4'}
for key in _dict:
print(colNames.index(key))
Answered By - Zain Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.