superhero = {'name': 'Deadpool', 'universe':'marvel', 'suit-color':'red'}
superhero['universe']
In list, order matters, but in dictionary order doesn't matter.
i.e Dictionary is unordered
[1, 2, 3] == [2, 3, 1]
{1, 2, 3} == {2, 3, 1}
'name' in superhero
'Deadpool' in superhero
variables equated to dictionaries are merely references to the original dictionary stored in memory. Just like in lists. For more info, see list and strings notebook.
superhero.keys()
lets convert that to list
list(superhero.keys())
list(superhero.values())
list(superhero.items())
superhero
for This_is_random_variable in superhero.keys():
print(This_is_random_variable)
for This_is_random_variable in superhero.values():
print(This_is_random_variable)
for This_is_random_variable in superhero.items():
print(This_is_random_variable)
The values returned above are 3 tuples. Tuples are same things as lists except they are immutable and uses round brackets
for k,v in superhero.items():
print(k, v)
'Deadpool' in superhero.values()
get( , ) method is used to get value of specific key in the dictionary. The key whose value we want to get in the first argument, and if that key doesnt't exist, it returns the second argument value
superhero.get('suit-color', "This key doesn't exist")
superhero.get('power', "This key doesn't exist")
superhero
if 'power' not in superhero:
superhero['power'] = 'healing'
superhero
del superhero['power'] #deletes the power key and its value
superhero
Above code can be done using setdefault() using one line. It defines new key value pair in the dictionary if that key doesn't exist yet. If that key is already there, then it does nothing.
It is used to make sure a certain key is present in the dictionary
superhero
superhero.setdefault('power', 'healing')
superhero
Now if we do that again with another value, it won't do anything
superhero.setdefault('power', 'funny')
superhero
I will now write a code that tells the number of times each letter appeared in the string
someString = 'And she looked at the sky and wondered is this all there is?'
count = {}
for char_variable in someString: #iterates over each character in the string same as in list
count.setdefault(char_variable, '0') #adds the value of 0 to the character when a new character is encountered
count[char_variable] = count[char_variable] + 1
count
Oops! An error. I will just leave it here so in future I won't do that again. The error is that I enclosed the 0 in quotation marks, making it string, and we can't add an integer to the string.
using int(count[char_variable]) would have also worked.
someString = 'And she looked at the sky and wondered is this all there is?'
count = {}
for char_variable in someString: #iterates over each character in the string same as in list
count.setdefault(char_variable, 0) #adds the value of 0 to the character when a new character is encountered
count[char_variable] = count[char_variable] + 1
count
New bug free code that counts upper and lower alphabets as same by converting them all to upper letters.
someString = 'And she looked at the sky and wondered is this all there is?'
count = {}
for char_variable in someString.upper(): #iterates over each character in the string same as in list
count.setdefault(char_variable, 0) #adds the value of 0 to the character when a new character is encountered
count[char_variable] = count[char_variable] + 1
count
pprint or 'pretty print' can be used to get pretty output of a list or dictionary.
import pprint
pprint.pprint(count)
The pformat function in pprint module is used to get pretty output in the string format.
import pprint
pprint.pformat(count)
type(count)
type('')
type(42)
type(True)
type(4 == 4)
type(print())
somelist = [1,2,4]
type(somelist)