Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Andrew, use this link:
- # https://lmgtfy.com/?q=python+how+to+loop+through+attributes+in+a+dictionary
- # and for help with syntax, check out w3schools:
- # https://www.w3schools.com/python/python_for_loops.asp
- def my_filter():
- mydict = {"a": 1, "b": 2, "c": 3}
- for thing in mydict.items():
- print(thing)
- # outputs:
- # ('a', 1)
- # ('b', 2)
- # ('c', 3)
- def my_filter():
- mydict = {"a": 1, "b": 2, "c": 3}
- for attr, val in mydict.items():
- print(attr)
- print(val)
- # outputs:
- # a
- # 1
- # b
- # 2
- # c
- # 3
- def my_filter(limit):
- mydict = {"a": 1, "b": 2, "c": 3}
- for attr, val in mydict.items():
- if val <= limit:
- print(val)
- my_filter(2)
- # outputs:
- # 1
- # 2
- # takes in a limiting value, some integer
- # returns all values from a dictionary that are
- # less than or equal to the limit.
- def my_filter(limit):
- # dictionary to search over
- mydict = {"a": 1, "b": 2, "c": 3}
- # dictionary holding matches
- output = {}
- for attr, val in mydict.items():
- if val <= limit:
- output[attr] = val
- return output
- matches = my_filter(2)
- print(matches)
- # outputs:
- # {'a': 1, 'b': 2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement