I am creating a loop in order to append continuously values from user input to a dictionary but i am getting this error:
AttributeError: 'dict' object has no attribute 'append'
This is my code so far:
for index, elem in enumerate(main_feeds):
print(index,":",elem)
temp_list = index,":",elem
li = {}
print_user_areas(li)
while True:
n = (input('\nGive number: '))
if n == "":
break
else:
if n.isdigit():
n=int(n)
print('\n')
print (main_feeds[n])
temp = main_feeds[n]
for item in user:
user['areas'].append[temp]
Any ideas?
The python AttributeError: 'dict' object has no attribute 'append' error occurs when you try to append a value in a dict object. The dict should be modified as list or the values should be added as key value in the dict object. Before calling the append method, the object type should be verified.
The Python "AttributeError: 'NoneType' object has no attribute 'append'" occurs when we try to call the append() method on a None value, e.g. assignment from a function that doesn't return anything. To solve the error, make sure to only call append() on list objects.
Appending element(s) to a dictionary To append an element to an existing dictionary, you have to use the dictionary name followed by square brackets with the key name and assign a value to it.
In the dictionary extend() means to concatentates the first dictionary with another dictionary. In Python dictionary, the append() function is used to add elements to the keys in the dictionary. In the dictionary extend() means to concatenate the first dictionary with another dictionary.
Like the error message suggests, dictionaries in Python do not provide an append operation.
You can instead just assign new values to their respective keys in a dictionary.
mydict = {}
mydict['item'] = input_value
If you're wanting to append values as they're entered you could instead use a list.
mylist = []
mylist.append(input_value)
Your line user['areas'].append[temp]
looks like it is attempting to access a dictionary at the value of key 'areas'
, if you instead use a list you should be able to perform an append operation.
Using a list instead:
user['areas'] = []
On that note, you might want to check out the possibility of using a defaultdict(list)
for your problem. See here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With