FAQ: Learn Python: Python Lists and Dictionaries - It's Dangerous to Go Alone! Take This

This community-built FAQ covers the “It’s Dangerous to Go Alone! Take This” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise It’s Dangerous to Go Alone! Take This:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

3 posts were split to a new topic: Shorthand to Increase a Variable’s Value?

2 posts were split to a new topic: “AttributeError: ‘NoneType’ Object Has No Attribute ‘remove’.”

I know I must have missed something earlier or am not completely getting all the concepts but rather than put in additional code that asks to adjust/add/remove values,items, keys or variables why not just change the original code why create so much additional lines?

Hi

I don’t quite get this code:
inventory['gold'] = inventory['gold'] + 50

How does it know to add 50 to the value (500)? Shouldn’t this just add 50 to the key (gold)

Were you able to solve the problem?

inventory = {
‘gold’ : 500,
‘pouch’ : [‘flint’, ‘twine’, ‘gemstone’], # Assigned a new list to ‘pouch’ key
‘backpack’ : [‘xylophone’,‘dagger’, ‘bedroll’,‘bread loaf’]
}

inventory[‘burlap bag’] = [‘apple’, ‘small ruby’, ‘three-toed sloth’]

inventory[‘pouch’].sort()

inventory[‘pocket’] = [‘seashell’, ‘strange berry’, ‘lint’]
inventory[‘backpack’].sort()
inventory[‘backpack’].remove(‘dagger’)
inventory[‘gold’] = inventory[‘gold’] + 50

Hey! Is there something we can help you with?

So you can either use:
inventory[‘gold’] = inventory[‘gold’] + 50
or
inventory[‘gold’ += 50.

Than why isnt it for remove:
inventory[‘backpack’] -= ‘dagger’

1 Like

The value keyed to “gold” is an integer, which responds to the operators + and - in the expected way.

The value keyed to “backpack” is a list of strings. You can “add” (concatenate) a list to a list, or a string to a string using the + operator, but the - operator will throw a TypeError if used with them.

lst = ['abc', 'def', 'gh']
lst = lst + ['ijk']
print(lst)
lst = lst - ['ijk']
print(lst)

# Output:
['abc', 'def', 'gh', 'ijk']
Traceback (most recent call last):
  File "C:\path\to\test.py", line 4, in <module>
    lst = lst - ['ijk']
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Can you possible use the for item in list function to add 50 gold to the inventory? I tried it but at best got an error called Can't assign to literal.

I originally thought the instructions were to add the number 50 to gold so I typed in:

inventory[‘gold’].append(50)

and it threw back an error saying:

Traceback (most recent call last):
File “python”, line 20, in
AttributeError: ‘int’ object has no attribute ‘append’

Then I realized I wasn’t doing what it wanted anyway and so put in the right code no problem. But now I’m sitting here wondering why the append function isn’t working and what would work in this case?

Edit: To clarify, I want to add the number 50 to the list. So gold would show 500, 50; not 550

Thanks!