31

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?

DYZ
  • 55,249
  • 10
  • 64
  • 93
anonymous
  • 331
  • 1
  • 3
  • 3
  • 3
    Well, a dict doesn't have an append method. And even if it did, you couldn't call it with square brackets. – Daniel Roseman Jan 12 '18 at 21:56
  • use defaultdict instead (with list in it) – RandomB Jan 12 '18 at 21:59
  • 1
    You assign values to a dict using its key like this : `user['areas'] = temp`. Your code would only work IF `user[areas]` was already a list. If you need it to be a list, construct the list first, THEN assign that list to the key. – JacobIRR Jan 12 '18 at 22:00

4 Answers4

33

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

  • 11
    Dictionaries in Python _do_ provide an `update` method though. So, if you want to add more key-value pairs: `dict.update({'another_key': 'another_value'})`. Maybe of value here. `update` will overwrite existing keys of the same name, so _caveat emptor_. – Matt Morgan Jan 12 '18 at 22:15
  • Very true Matt! Although, it looks like they're trying to just append values here. – Shane Williamson Jan 12 '18 at 22:17
2

As the error suggests, append is not a method or attribute, meaning you cannot call append in the dictionary user. Instead of

user['areas'].append[temp]

Use

user['areas'].update[temp]
Nah
  • 189
  • 1
  • 11
1

As the error and the fellows pointed out, there is no 'append' attribute to a 'dict' object.

But if you really see a Python code with the line dict["key"].append(value) working, it is because there is a list inside this key, and you want to specifically add values in the list inside the key (not to add new entries to the key itself). Then dict["key"].append(value) will work anyway.

-6

Either use dict.setdefault() if the key is not added yet to dictionary :

dict.setdefault(key,[]).append(value)

or use, if you already have the keys set up:

dict[key].append(value)

source: stackoverflow answers

sivi
  • 10,654
  • 2
  • 52
  • 51