How to pop and add the value of a dictionary to another variable using a loop

Link to the lesson: https://www.codecademy.com/courses/learn-python-3/lessons/using-dictionaries/exercises/review-ii

In this exercise, we are told to pop the value assigned to the specified key in tarot dictionary and then assign it as a value to spread dictionary with the keys “past”, “present”, and “future” of spread.

So instead of writing it like

# spread["past"] = tarot.pop(13)
# spread["present"] = tarot.pop(22)
# spread["future"] = tarot.pop(10)

May I know how do I write it in a for loop? I keep getting either TypeError or KeyError each time I attempt it…

It helps if you post the code that DOESN’T work more so than the code that does…

Since no-one else has responded in ~2 hours and I have no idea what you loop looks like; I will give you mine, and go over what it does.

for key, value in spread.items():
  print("Your " + key + " is the " + value + " card.")

If you remember what you learned on the previous page in the lesson, you can get both the key and value from a dictionary and use it in a for loop using this syntax:

for key, value in dictionary.items():
  #do something

Using the syntax above, we can plug our dictionary in and what we want the loop to do into it, so we start with:

for key, value in spread.items():

Note- it doesn’t technically matter what you name the elements in your loop, but using key, value makes the code easier to read. Just make sure to give them unique names,

Then we can work on what the loop will actually do. Since we want to print a string that looks like this:
“Your {key} is the {value} card.”

We can easily add a single print statement to our loop using string concatenation. Which would look like:

print("Your " + key + " is the " + value + " card.")

Then all you have to do is combine them and voilà we get our completed loop.

for key, value in spread.items():
  print("Your " + key + " is the " + value + " card.")

EDIT - Spelling, grammar.

Hi, the solution you’ve provided is after popping and adding the values to spread. My question is about something like this:

NOTE: The code below will result in a TypeError

spread = {}

keys_to_pop = [13, 22, 10]

for time in ["past", "present", "future"]:
  spread[time] = tarot.pop(keys_to_pop)

You get that error because the list type is unhashable… you can convert your list into a tuple to remove the TypeError.

I played around with it a bit, I managed to remove the type error but now get a key error.

All and all I"m not quite sure how to do it like you’re asking… it’s not how they want you to do it in the exercise.

spread = {}

spread["past"] = tarot.pop(13)
spread["present"] = tarot.pop(22)
spread["future"] = tarot.pop(10)

Is the way the directions tell you to solve this

Yes, I’m completely aware of how they want me to solve this exercise… Anyways, I appreciate you trying to help, but maybe someone knows how to produce the result I want hopefully. Again, thank you :smiley:

1 Like

I’ll keep messing around with it, if I manage to fix it I’ll drop the code here.

draw = sample(range(1, 23), 3)
times = ["past", "present", "future"]
spread = {}
for d, t in zip(draw, times):
  spread[t] = tarot.get(d)

I completely forgot about zip till I did a search. I was trying for d, t in draw, times: but python didn’t get it.
There’s a few other ways too, just iterate on a range for list access, or a nested self consuming for loop.

Does the code use the random module? It’s got sample function in it, I think there’s already a simpler way to do this by using choice function instead of sample since we don’t have repeated values.

spread = {}

for time in ["past", "present", "future"]:
  # convert dict to list and get its keys
  spread[time] = tarot.pop(random.choice(tarot_cards_left))

Anyways, this isn’t what I originally asked, but thank you for helping out. :smiley:

1 Like

yeah it’s in random from random import sample