+ 2
How to use the get() method
Help v:
4 Antworten
+ 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.
+ 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
+ 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)".
0
Ty all!