Search code examples
pythonlistdictionarystring-length

Dictionary from List in Python based on length of list


I have the following list in python

 days = ['Saturday', 'Monday', 'Thursday', 'Friday', 'Tuesday', 'Sunday', 
        'Wednesday']

I want to create the following dictionary

dayweek = {'Saturday': 1, 'Monday': 2, 'Thursday': 3, 'Friday': 4, 'Tuesday': 
5, 'Sunday': 6, 'Wednesday': 7}

I have tried the following code

     dayweek=dict(enumerate(days))

This yields the following result

  {0: 'Saturday', 1: 'Monday', 2: 'Thursday', 3: 'Friday', 4: 'Tuesday', 5: 
  'Sunday', 6: 'Wednesday'}

How do i accomplish the same

I request someone to help me


Solution

  • check this out:

    dict([(j, i) for i, j in enumerate(days, 1)])