Python Dictionary Comprehension

def Convert(lst):res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}return res_dct# Driver codelst = ['a', 1, 'b', 2, 'c', 3]print(Convert(lst))
{'b': 2, 'c': 3, 'a': 1}
  • This “lst[i]” is the dictionary key and
  • Every thing here lst[i + 1] for i in range(0, len(lst), 2) is the value.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
george udosen

george udosen

45 Followers

DevOps | FullStack developer | Python::Flask | GCP Cloud Certified | AWS & AZURE Cloud Savy | Linux Sysadmin | Google IT Support