Home > Net >  Is there a way to update the value of a key in a dictionary, if the value of the dictionary is a lis
Is there a way to update the value of a key in a dictionary, if the value of the dictionary is a lis

Time:03-20

I have four lists of special character strings eg '™'. I am storing them in a dictionary as the value to the keys 1 through 4.

I am trying to loop through all items in each of the dictionary's lists and then update each value to its utf-8 encoded equivalent. Here's what I have so far:

li1 = ['€','‚','ƒ','„','…','†','ˆ','‰']
li2 = ['Š','‹','Œ','Ž']
li3 = ['‘','’','“','”','•','–','—','˜','™']
li4 = ['š','›','œ','ž','Ÿ']

uni_dic = {
    '1':li1,
    '2':li2,
    '3':li3,
    '4':li4
}

for key in uni_dic:
    for val in uni_dic[key]:
        uni_dic.update({key,val.encode('utf-8')})

This returns a 'ValueError: dictionary update sequence element #0 has length 1; 2 is required'

CodePudding user response:

Take out the last three lines and write:

for key, values in uni_dic.items():
    uni_dic[key] = [string.encode('utf-8') for string in value]

or in one line:

uni_dic = {key: [string.encode('utf-8') for string in value] for key in uni_dic}

CodePudding user response:

In this case, you can create another list like below:

for key in uni_dic:
    uni_dic[key] = [val.encode('utf-8') for val in uni_dic[key]]

If you want to do it in-place:

for key in uni_dic:
    for idx, val in enumerate(uni_dic[key]):
        uni_dic[key][idx] = val.encode('utf-8')

CodePudding user response:

The syntax error is that you're specifying your update dict as {key,val.encode('utf-8')} (which is actually a set) instead of {key: val.encode('utf-8')}. However, it's a bit silly to use update to update a single key anyway, and they way you're looping through the list, you'll just be overwriting the list repeatedly with each element in turn (ultimately ending up with the last item).

Instead, build a new list out of the encoded values, and assign the entire list to uni_dic[key]:

for key in uni_dic:
    encoded_vals = []
    for val in uni_dic[key]:
        encoded_vals.append(val.encode('utf-8'))
    uni_dic[key] = encoded_vals 

Using a list comprehension to build encoded_vals gets you the much more concise:

for key in uni_dic:
    uni_dic[key] = [val.encode('utf-8') for val in uni_dic[key]]

And using a dictionary comprehension with dict.items() makes it even simpler:

uni_dic = {k: [val.encode('utf-8') for val in v] for k, v in uni_dic.items()}

CodePudding user response:

A one-line solution to your question is:

uni_dic = {key:[val.encode('utf-8') for val in charList] for key, charList in uni_dic.items()}

To get the same result with a minimal modification to the code in your question, you could do this:

li1 = ['€','‚','ƒ','„','…','†','ˆ','‰']
li2 = ['Š','‹','Œ','Ž']
li3 = ['‘','’','“','”','•','–','—','˜','™']
li4 = ['š','›','œ','ž','Ÿ']

uni_dic = {
    '1':li1,
    '2':li2,
    '3':li3,
    '4':li4
}

for key in uni_dic:
    charList = uni_dic[key]
    for i, val in enumerate(charList):
        charList[i] = val.encode('utf-8')
[print(charList) for charList in uni_dic.values()]

Output:

[b'\xe2\x82\xac', b'\xe2\x80\x9a', b'\xc6\x92', b'\xe2\x80\x9e', b'\xe2\x80\xa6', b'\xe2\x80\xa0', b'\xcb\x86', b'\xe2\x80\xb0']
[b'\xc5\xa0', b'\xe2\x80\xb9', b'\xc5\x92', b'\xc5\xbd']
[b'\xe2\x80\x98', b'\xe2\x80\x99', b'\xe2\x80\x9c', b'\xe2\x80\x9d', b'\xe2\x80\xa2', b'\xe2\x80\x93', b'\xe2\x80\x94', b'\xcb\x9c', b'\xe2\x84\xa2']
[b'\xc5\xa1', b'\xe2\x80\xba', b'\xc5\x93', b'\xc5\xbe', b'\xc5\xb8']
  • Related