Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my_li = ['a', 'b', 'asdf', '9876']
- for element in my_li:
- print(f"Element: {element} has the index of {my_li.index(element)}") # list.index() is O(n)
- for element in my_li:
- for element_2 in my_li:
- if element_2 == element:
- pass
- for i, element in enumerate(my_li):
- print(f"Element: {element} has the index of {i}")
- my_dict = {'key': 1, "key2": "some val", "key3": "val"}
- print(my_dict['key']) # O(1)
- # time complexity O(n)
- if "key3" in my_dict.keys():
- print("key3 in the dictionary")
- else:
- print("key3 NOT in the dictionary")
- # time complexity O(1)
- try:
- print(f"key3 with value of {my_dict['key3']} in the dictionary")
- except KeyError:
- print("key3 NOT in the dictionary")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement