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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![E. T.](https://secure.gravatar.com/avatar/ac5d4a838c9c214f12e68bdd6392edb8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
E. T.
Python Development Techdegree Student 884 PointsHow to print inventory as a list rather than printing its items one at a time?
how to write a method for that?
3 Answers
![Megan Amendola](https://uploads.teamtreehouse.com/production/profile-photos/7340382/micro_headshot.jpg)
Megan Amendola
Treehouse TeacherI can help better if you post your code E.T.
When I use the code I wrote above it works fine:
class Inventory:
def __init__(self):
self.inventory = []
def get_inventory(self):
return self.inventory
def add_item(self, item):
self.inventory.append(item)
inventory = Inventory()
inventory.add_item('grapes')
inventory.add_item('bananas')
print(inventory.get_inventory())
---Console---
['grapes', 'bananas']
If you're getting object locations, then I am assuming your list is filled with objects of another class. Something like this:
class Inventory:
def __init__(self):
self.inventory = []
def get_inventory(self):
return self.inventory
def add_item(self, item):
self.inventory.append(item)
class Item:
def __init__(self, item):
self.item = item
def __str__(self):
return self.item
def __repr__(self):
return self.item
inventory = Inventory()
inventory.add_item(Item('grapes'))
inventory.add_item(Item('bananas'))
# this uses __repr__
print(inventory.get_inventory())
# this uses __str__
for item in inventory.inventory:
print(item)
---Console---
[grapes, bananas]
grapes
bananas
That means you need to add a __str__
method to access each item in your list. If you want to see your list print to the console, then you'll need a __repr__
. Here's some more detail on the two. Print a list works differently than just printing out an individual object.
![Megan Amendola](https://uploads.teamtreehouse.com/production/profile-photos/7340382/micro_headshot.jpg)
Megan Amendola
Treehouse TeacherHi, E. T.
You can add a method to your class to print()
or return
the list depending on what you want to do with it. For example:
class Inventory:
def __init__(self):
self.inventory = []
def get_inventory(self):
return self.inventory
![E. T.](https://secure.gravatar.com/avatar/ac5d4a838c9c214f12e68bdd6392edb8?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
E. T.
Python Development Techdegree Student 884 PointsMegan Amendola Still not getting it. I want to print the list. I tried after adding get_inventory() method:
>>> inventory.slots
<items.Item object at [address] >, <items.Item object at [address]>
It's giving me the addresses rather than showing what items are in that list!
![E. T.](https://secure.gravatar.com/avatar/ac5d4a838c9c214f12e68bdd6392edb8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
E. T.
Python Development Techdegree Student 884 PointsMegan Amendola Thanks a ton :)
If you want to see your list print to the console, then you'll need a
__repr__
.....Print a list works differently than just printing out an individual object.
This helps.