+ 2

How to use the get() method

Help v:

26th Nov 2024, 11:36 PM
Marcos
Marcos - avatar
4 Respuestas
+ 11
Hello Marcos this method is used for dictionaries. get.(key, value) key -> required, returns the value of the key value -> optional, a value to return if the key does not exist dict = {"a":1,"b":2} print(dict.get("a")) #output: 1 print(dict.get("c")) #output: None print(dict.get("c",0)) #output: 0 Hope this helps.
26th Nov 2024, 11:53 PM
Denise Roßberg
Denise Roßberg - avatar
+ 9
The get() method in Python is used with dictionaries to retrieve the value associated with the given key. Syntax looks like this: dictionary.get(key,default_value) key:The key we want to lookup in dictionary. default_value:This is optional and returns the provided value if key isn't found.If omitted it returns None. It's also bit safer than directly accessing key using dictionary[key] as it doesn't raise a KeyError if the key isn't present.Instead it returns a default value or None if default value isn't provided. https://sololearn.com/compiler-playground/czimJIHJ93gP/?ref=app
27th Nov 2024, 12:11 AM
⚡NB⚡
⚡NB⚡ - avatar
+ 1
hy@ Marcos..To use the "get()" method, you access it on a dictionary object, provide the key you want to retrieve the value for, and optionally specify a default value to return if the key doesn't exist in the dictionary; the syntax is: "dictionary.get(key, default_value)".
27th Nov 2024, 12:22 PM
Angel
0
Ty all!
27th Nov 2024, 1:07 PM
Marcos
Marcos - avatar