-
-
Save toyeiei/2498944a9b3214780c565a31a425b849 to your computer and use it in GitHub Desktop.
Python tutorial - write simple function to count item in a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# write a reusable function | |
def count_item(input_list): | |
"""count item in a list, return a dict""" | |
result = {} | |
for item in input_list: | |
if item in result: | |
result[item] += 1 | |
else: | |
result[item] = 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment