Re: fast dictionary initialization
On 2016-06-10 14:07, maurice wrote: > example: > valuesList = [1,2,3] > keysList = ['1','2','3'] > > So the dictionary can basically convert string to int: > > dictionary = {'1':1, '2':2, '3':3} A couple similar options: The most straightforward translation of your description: opt1 = dict(zip(keysList, valuesList)) print(opt1["2"]) And one where you generate the strings on the fly: opt2 = dict((str(i), i) for i in range(1, 4)) print(opt2["2"]) And one where you use the int() function instead of a mapping because the whole idea of storing a dict worth of string-numbers-to-numbers seems somewhat silly to me: print(int("2")) -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: fast dictionary initialization
pOn Fri, Jun 10, 2016, at 17:07, maurice wrote: > Hi. If I have already a list of values, let's call it valuesList and the > keysList, both same sized lists, what is the easiest/quickest way to > initialize a dictionary with those keys and list, in terms of number of > lines perhaps? > > example: > valuesList = [1,2,3] > keysList = ['1','2','3'] > > So the dictionary can basically convert string to int: > > dictionary = {'1':1, '2':2, '3':3} dict(zip(keysList, valuesList)) -- https://mail.python.org/mailman/listinfo/python-list
fast dictionary initialization
Hi. If I have already a list of values, let's call it valuesList and the keysList, both same sized lists, what is the easiest/quickest way to initialize a dictionary with those keys and list, in terms of number of lines perhaps? example: valuesList = [1,2,3] keysList = ['1','2','3'] So the dictionary can basically convert string to int: dictionary = {'1':1, '2':2, '3':3} Thanks -- https://mail.python.org/mailman/listinfo/python-list