Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Build an Application Add Items

Anthony Costanza
Anthony Costanza
2,123 Points

Getting all kinds of errors -

Following the tutorial - not sure where I'm going wrong - ugh!

Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

Anthony, could you cut-an-paste the error messages into a reply? That would be a good starting point to solving your issue.

Anthony Costanza
Anthony Costanza
2,123 Points

Traceback (most recent call last):
File "/home/treehouse/workspace/shopping_list.py", line 32, in <module>
add_to_list(new_item)
File "/home/treehouse/workspace/shopping_list.py", line 8, in add_to_list
shopping_list.append(item)
AttributeError: 'dict' object has no attribute 'append'

Anthony Costanza
Anthony Costanza
2,123 Points

Thank you guys !

But im a bit confused - I'm just following the assignment on Tree House - doing exactly what they are telling me to do - it works for the Treehouse instructor -

Here is the code:

shopping_list = {}

def add_to_list(item):

shopping_list.append(item)

print("Added! List has {} items.".format(len(shopping_list)))

def show_help(): print("What should we pick up at the store?") print(""" ENTER 'DONE' to stop adding items. Enter 'HELP' for this help. """)

show_help()

while True: new_item = input("> ")

if new_item == 'DONE':
    break
elif new_item == 'HELP':
    show_help()
    continue

add_to_list(new_item)
Anthony Costanza
Anthony Costanza
2,123 Points

Isn't that what I have as my first line of code?

Anthony Costanza
Anthony Costanza
2,123 Points

Oh! I have {} INSTEAD of [] - ugh!

so... the two are very different ?

Anthony Costanza
Anthony Costanza
2,123 Points

Thank you!

Hopefully this does become second nature - but currently - I'm not convinced

Seems daunting

6 Answers

AJ Tran
STAFF
AJ Tran
Treehouse Teacher

Hi there. Let's take a look at that last line of the error message:

AttributeError: 'dict' object has no attribute 'append'

The source of the error is this line of code:

shopping_list.append(item)

The error is telling us that shopping_list is a Python Dictionary, which does not have a built-in method called .append() !

You can use .append() to add items to a Python List, but not a Python Dictionary.

To add a new key-value pair to a Dictionary, observe my examples below:

# Add by using bracket-notation

inventory = {
    "apples": 1,
    "bananas": 2
}

inventory["milk"] = 3

print(inventory)

# => {"apples": 1, "bananas": 2, "milk": 3}
# Add by using .update()

inventory = {
    "apples": 1,
    "bananas": 2
}

new_item = { "milk": 3 }

inventory.update(new_item)

print(inventory)

# => {"apples": 1, "banana": 2, "milk": 3}
Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

You beat me by a minute... good answer by the way! JM

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Craig is asking you to create a new empty list called shopping_list:

shopping_list = [] # create a new empty list with no items

But you can also do it like this:

shopping_list = list() # this is equivalent to the previous declaration

That should solve your issue.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Look carefully and you will see square brackets rather than curly braces. You can also use the list() declaration like I showed in the second example.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Ok, those messages do help me to know what is going on in your code.

One of the things I love about Python, well all computer languages in general is that there are numerous ways to write a program correctly. So you can inject your ideas and creativity in approaching problems.

The Error itself that is key is this-- "AttributeError: 'dict' object has no attribute 'append'"

Dictionary objects don't have an append() method, but List objects do. Here's an example below...

shopping_list = [] # note the square brackets is an empty list
shopping_list.append('milk') # append milk...
shopping_list.append('eggs') # eggs...
shopping_list.append('bread') # bread

print(shopping_list)

The result is this.

['milk', 'eggs', 'bread']

As I mentioned above, you can write a project that is completely different and uses Dictionary objects which would work fine as well. However, you won't use append(), but can use a key/value approach. The key is in the square bracket, and then we assign the key a string value, like milk, bread, or eggs! See below

shopping_list = {} # note the curly braces is an empty dict
shopping_list[0] = 'milk' # add 0/milk key value...
shopping_list[1] = 'eggs' # add 1/eggs...
shopping_list[2] = 'bread' # add 2/bread

print(shopping_list)

And so we have a dictionary that contains similar information, but structured a little differently!

{0: 'milk', 1: 'eggs', 2: 'bread'}

I hope this helps!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

It can definitely be confusing at first, but it will become second nature as you will use both dictionaries and list objects quite a bit.

Another cool structure is the set() and the set is kind of like a list but doesn't allow for repeated elements and that can be super handy. And the set uses curly braces like the dictionary, but you will come upon that soon enough.

Good luck on your Python journey!!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Repetition is the key-- program a little every day and it will be your new friend. When I feel overwhelming stress (I'm looking at you 2020!) I will pick a quick project idea and hack away. My mind goes to an entirely different, calm place with Ruby, Python, or JavaScript as my toolkit! Yeah, I'm geeky like that.

Also... you should check out the website

https://repl.it

Just in case you don't have your computer all set up for Python, you can go there and have Python workspaces. For example, you could put the Shopping List project there or anything else you are really proud of and you can share your code pretty easily with potential employers or clients.

Best of luck on your journey!