Trying to assign 2 values or more to a dictionary key using variables?

Fader

Sorry, i'm a bit new to python and any help is appreciated.

I'm trying to assign 2 values to a dictionary key using a variable that is given a value when prompted. I keep getting this error message:

Traceback (most recent call last):
File "C:\Users\Fader\Desktop\List creator.py", line 41, in <module>
myLists[key].append(edit)
AttributeError: 'str' object has no attribute 'append'

This is what I have so far:

    print (list(myLists.keys()))
    key=input("What List would you like edit?")
    myLists.setdefault(key, [])
    edit=input("What would you like to add?")
    myLists[key].append(edit)

Im trying to prompt for a key. Set that key as the default and then prompt for an edit and append the edit to the specified key.

Israel Unterman

I think instead of default you mean initial value.

Please change the third line to this, and check if this is what you meant:

myLists[key] = []

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign multiple values to a key in a dictionary, multiple times using pandas

Using input to find a key in a dictionary and assign the values to that variable

Can we assign 2 types of values to a key in a dictionary

How to assign multiple values to key in dictionary

Use of unassigned variable error when I am trying to assign values to variables using IF statements

Assign selected values to Pandas Series using dictionary

Trying to grow a nested dictionary by adding more key:value pairs

Creating variables from a python dictionary with multiple values for each key using a for loop

Extract key1:value1 from dictionary 1 and key1:value1 from dictionary 2, assign them to 4 different variables, loop

Assign dictionary values returned from class method to class variables

How to assign all values in dictionary to variables based on max value

Using dropDown menu to assign values to variables with knockout

Using variables in a for loop to assign legend values in pyplot

How to assign two new key values in a JObject from a dictionary?

How to use values from 2 columns in my dataframe to assign a new column using a dictionary

Adding more than one value to a Key in a Dictionary with Tuple Values

Is there a more efficient way in incrementing values of every key in a dictionary?

c#: Trying to inject dictionary key and values into an Excel file

Issue trying to return key and values inside a nested dictionary

Add values to dictionary key using loop

Get key values of a generic Dictionary <,> using reflection

Using Python dictionary values to return key as result

Sort list using dictionary key values

Using regex to break string into a dictionary with key and values

Using a TypeConverter for a Dictionary key, but not Dictionary values of the same type?

Python using dictionary list values to key match a dictionary

How to access the key values in a nested python dictionary with variables?

Using python comprehension with dictionary to assign values to pandas dataframe

Using **kwargs to assign dictionary keys + values to a class instance