Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Syntax: D.method()
- # Where D = dictionary, dict
- # D.clear() -> None.
- # Remove all items from D.
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': '2012'
- }
- print(car)
- # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
- print(car.clear())
- {}
- # D.copy -> a shallow copy of D
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': '2012'
- }
- print(car)
- # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
- x = car.copy()
- print(x)
- # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
- # D.fromkeys(iterable, [value=None, /])
- # '/' marks preceding args as positional-only.
- # Create a new dictionary with keys from iterable and values set to value.
- keys = ('key1', 'key2', 'key3')
- values = 0
- thisdict = dict.fromkeys(keys, values)
- print(thisdict)
- # ['key1': 0, 'key2': 0, 'key3': 0]
- thisdict = dict.fromkeys(keys)
- print(thisdict)
- # ['key1': None, 'key2': None, 'key3': None]
- # D.get(key, [default=None, /])
- # '/' marks preceding args as positional-only.
- # Return the value for key if key is in the dictionary, else default.
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': '2012'
- }
- get_key = car.get("model")
- print(get_key)
- # model
- get_value = car.get("price", 38990)
- print(get_value)
- # 38990
- # D.items() -> a set-like object providing a view on dict's items
- car = {
- 'brand': 'Telsla',
- 'model': 'Model X',
- 'year': 2012
- }
- thisdict_items = car.items()
- print(thisdict_items)
- # dict_items([('brand', 'Telsla'), ('model', 'Model S'), ('year', 2012)])
- car["year"] = 2019
- print(thisdict_items)
- # dict_items([('brand', 'Telsla'), ('model', 'Model X'), ('year', 2019)])
- # D.keys() -> a set-like object providing a view on dict's keys
- thisdict_keys = car.keys()
- print(thisdict_keys)
- # dict_keys(['brand', 'model', 'year'])
- car["color"] = "White"
- print(thisdict_keys)
- # dict_keys(['brand', 'model', 'year', 'color'])
- # D.values() -> an object providing a view on D's values
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': 2019
- }
- thisdict_values = car.values()
- print(thisdict_values)
- # dict_values(['Telsla', 'Model S', 2019])
- thisdict_values = car.values()
- car['year'] = 2020
- print(thisdict_values)
- # dict_values(['Telsla', 'Model S', 2020])
- # D.pop(dict.pop(k[,d]) -> v, remove specified key and return the corresponding value.
- # If key is not found, d is returned if given, otherwise keyError is raised
- car = {
- 'brand': 'Telsla',
- 'model': 'Model X',
- 'year': 2019
- }
- car.pop('model')
- print(car)
- # {'brand': 'Telsla', 'year': 2019}
- car = {
- 'brand': 'Telsla',
- 'model': 'Model X',
- 'year': 2019
- }
- car.pop('model', 'Model X')
- print(car)
- # {'brand': 'Telsla', 'year': 2019}
- # D.popitem() -> (k, v), remove and return som (key, value) pair as a
- # 2-tuple; but raise KeyError if D is empty.
- car = {
- 'brand': 'Telsla',
- 'model': 'Model X',
- 'year': 2019
- }
- car.popitem()
- print(car)
- # {'brand': 'Telsla', 'model': 'Model X'}
- # D.setdefault(key, [default=None, /])
- # '/' marks preceding args as positional-only.
- # Insert key with a value of default if key is not in the dictionary.
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': 2019
- }
- set_default_value = car.setdefault("model", "Model X")
- print(set_default_value)
- # Model S
- print(car)
- # {'brand': 'Telsla', 'model': 'Model S', 'year': 2019}
- # D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
- # If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
- # If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
- # In either case, this is followed by: for k in F: D[k] = F[k]
- car = {
- 'brand': 'Telsla',
- 'model': 'Model S',
- 'year': 2019
- }
- car.update({"color": "White"})
- print(car)
- # {'brand': 'Telsla', 'model': 'Model S', 'year': 2019, 'color': 'White'}
- '''To get a list of all built-in dictionary
- methods or objects in Python you will need to use the
- 'Tab' or 'control + Space' key on your keyboard after the
- two quotation marks and period (.)
- like this {}. {} is used to dictionary.
- -->''.'wait a few seconds to see the methods' or ''.'Tab' or ''.'control + Space'<--
- You can replace "{}" with 'dict'.
- Syntax: dict.method() where dict is the dictionary objects
- Python dictionary methods takes the form dict.method(). Where
- dict is the dictionary object.
- The period (.) on the method makes a function become a method because
- the function now
- runs on the object, dict.
- This means that a function can be a method but a method can't be a function.
- To see the complete methods on a list type 'help(dict)' or 'help('')'.
- Syntax: dict.method() or dict.method(object) in some cases.
- Where dict is the dict object.
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement