Skip to content
Advertisement

Pythonic method to create 2 lists, one of which is a repeating set of strings and turn into a dict

I have a list of ‘widget types’, [“C”,”A”,”B”] and need to make a dict for those types to correspond to their respective ID’s, 1=’C’, 2=’A’, 3 = ‘B’, 4= ‘C’, 5=’A’, 6= ‘B’, 7 = ‘C’, etc. I already know how to do it, I just wondered if there was a more elegant pythonic way of achieving it than that below, also it would be good to have the length of the dict/lists expandable, i.e. in the example it is 10 but in reality it would be many hundreds of widgets long.

Existing working code

JavaScript

Advertisement

Answer

JavaScript

Dict comprehension above may be your quest item, wid is wid_ID_dict in your code.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement