0

I have a list that I am trying to convert into a dictionary, but for some reason one of the expected key-value pairs is missing from the resultant dictionary and I can't figure out why.

le_list = ['Social security and child support:', 'sscsa-glasgow@justice.gov.uk', 'Enquiries:', 'enquiries-cicap@hmcts.gsi.gov.uk', 'Social security and child support:', '0300 790 6234', 'Social security and child support fax:', '01264 347 981']
for i in le_list:
    k = le_list[0::2]
    v = le_list[1::2]

le_dict = dict(zip(k, v))
print(le_dict)

{'Enquiries:': 'enquiries-cicap@hmcts.gsi.gov.uk', 'Social security and child support fax:': '01264 347 981', 'Social security and child support:': '0300 790 6234'}

When I do a simple test with another list, it works fine:

le_list = ['A:', '1', 'B:', '2', 'C', '3', 'D', '4']
for i in le_list:
   k = le_list[0::2]
   v = le_list[1::2]

le_dict = dict(zip(k, v))
print(le_dict)

{'C': '3', 'A:': '1', 'D': '4', 'B:': '2'}

What is causing [u'Social security and child support:', u'sscsa-glasgow@justice.gov.uk'] not to be included as a dict key-value pair?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
ron_g
  • 1,474
  • 2
  • 21
  • 39
  • 3
    You can't have multiple keys which are the same thing. See your first and third key. – cs95 Apr 12 '18 at 08:59
  • @cᴏʟᴅsᴘᴇᴇᴅ I was looking at `Social security and child support:` and thinking "it's exactly the same, why would it work with one and not the other?". Thanks - a lesson learned! – ron_g Apr 12 '18 at 09:02
  • yes @coldspeed is correct! its value is updated and thus its giving you the latest value! – Amitkumar Karnik Apr 12 '18 at 09:09

0 Answers0