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

Would someone walk through this python function with me to better understand it dealing with dict

Could someone walk through this with me, maybe adding #s notes between each line?

many thanks. I've been doing really well, but had to move on from this exercise and I just want to make sure I fully understand it

Write a function named members that takes two arguments, a dictionary and a list of keys. Return a count of how many of the items in the list are also keys in the dictionary.

The function i found to work was

def members(my_dict, my_list):
  total = 0
  for x in my_list:
      if x in my_dict:
         total += 1
  return total

Now I kinda get the function operation, but how could I get this fuction to run in an actual program

would I also create var for my_dict and my_list inside the function or would I create like a input var to add to my_dict and my_list

how would I actually get it to show my result? print members or print total?

Maybe I should go back to the shopping list python function exercise and try to hybrid this dict exercise into it?

Sorry for the rambling, my brain is numb right now

2 Answers

"how would I actually get it to show my result? print members or print total?"

You can do this to show output:

a_dict = {'animal':'dog', 'name':'spark'}
a_list = ['animal', 'age']
matches = members(a_dict, a_list)
print("There are {} keywords in your list.".format(matches))

EXCELLENT!! Many thanks!!

def members(my_dict, my_list): total = 0 for x in my_list: if x in my_dict: total += 1 return total

Now I kinda get the function operation, but how could I get this fuction to run in an actual program

Call the function. For example, in the main part of the program:

create variables for dictionary and list

el_dicto = {1: "monkey", "tower": "skunk", "pants": 5} el_listo = ["pants", "shirt", "socks"]

now call the function

members(el_dicto, el_listo)

would I also create var for my_dict and my_list inside the function or would I create like a input var to add to my_dict and my_list No. You don't need to create the variable within the function. Putting it in the parentheses when you call the function will make it a variable inside the function.

how would I actually get it to show my result? print members or print total? If you want to print from within the function, you can do it like this: def members(my_dict, my_list): total = 0 for x in my_list: if x in my_dict: total += 1 print total return total

If you want to print it outside the function...like this: print members(el_dicto, el_listo)

Or assign the results of the function to a variable and then print it:

results = members(el_dicto, el_listo) print results

You can't print "total" outside of the function, because it doesn't exist anymore...it only exists within the function "members."

Awesome!! This was a great help to me!!!