简体   繁体   中英

Adding/editing list of strings as values of a dictionary (Python)

I need help with adding lists of strings to key/value pairs in a dictionary. Basically, I want to get a user input, and if the user input does not already exist as one of the keys in the dictionary, I want to create a new key:value pair corresponding to the user input. If the user input already exists as a key in the dictionary, I want to add more information to its corresponding value.

Here is what I have tried so far:

names1 = [‘string1’, ‘string2’, ‘string3’]
names2 = [’string4’, ‘string5’, ‘string6’]
names3 = [’string7, ‘string8’, ‘string9’]

a_dict = {‘list_one’: names1,
          ‘list_two’: names2,
          ‘list_three’: names3}

new_list = input(‘enter list name: ’)
new_string = input(‘enter word: ‘)

existing_lists = a_dict.keys()
existing_strings = a_dict.values()

if x in existing_lists: #if the animal type alrady exists
    for key, value in a_dict.items(): #for the key that is equal to the new pet's type
        if key == x:
            value = value.extend(x)
    else:
        a_dict.update({new_list : new_string})
print(a_dict)

DESIRED output example 1:

enter list name: list_one
enter word: string10

a_dict = {‘list_one’: [‘string1’, ‘string2’, ‘string3’, ‘string 10’],
                ‘list_two’: [’string4’, ‘string5’, ‘string6’],
                ‘list_three’: [’string7, ‘string8’, ‘string9’],

DESIRED output example 2:

enter list name: list_four
enter word: ‘string10’

a_dict = {‘list_one’: [‘string1’, ‘string2’, ‘string3’],
                ‘list_two’: [’string4’, ‘string5’, ‘string6’],
                ‘list_three’: [’string7, ‘string8’, ‘string9’],
                ‘list_four’ : [‘string10’]}

The output I am currently getting completely replaces the existing value with the user input instead, such as:

enter list name: list_one
enter word: string10

a_dict = {‘list_one’: [‘string 10’],
          ‘list_two’: [’string4’, ‘string5’, ‘string6’],
          ‘list_three’: [’string7, ‘string8’, ‘string9’]}

I really appreciate the help!

If the key exist, use append If not, create the key and store the value in a list

names1 = ['string1', 'string2', 'string3']
names2 = ['string4', 'string5', 'string6']
names3 = ['string7', 'string8', 'string9']

a_dict = {'list_one': names1,
          'list_two': names2,
          'list_three': names3}

new_list = input('enter list name: ')
new_string = input('enter word: ')


if new_list in a_dict:
    a_dict[new_list].append(new_string)
else:
    a_dict[new_list] = [new_string]

print(a_dict)

There were few bugs in the code for variables used. And the else was misplaced. Also, you need to use append instead of extend.

names1 = ['string1', 'string2', 'string3']
names2 = ['string4', 'string5', 'string6']
names3 = ['string7', 'string8', 'string9']

a_dict = {'list_one': names1,
          'list_two': names2,
          'list_three': names3}

new_list = input('enter list name: ')
new_string = input('enter word: ')

existing_lists = a_dict.keys()
existing_strings = a_dict.values()

if new_list in existing_lists: #if the animal type alrady exists
    for key, value in a_dict.items(): #for the key that is equal to the new pet's type
        if key == new_list:
            value = value.append(new_string)
else:
    a_dict.update({new_list : new_string})
print(a_dict)

one-liner:

new_list = input('enter list name: ')
new_string = input('enter word: ')

a_dict[new_list] = a_dict.get(new_list,[]) + [new_string]

print(a_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM