poplardsky
0
Q:

python dictionary

#Creating dictionaries
dict1 = {'color': 'blue', 'shape': 'square', 'volume':40}
dict2 = {'color': 'red', 'edges': 4, 'perimeter':15}

#Creating new pairs and updating old ones
dict1['area'] = 25 #{'color': 'blue', 'shape': 'square', 'volume': 40, 'area': 25}
dict2['perimeter'] = 20 #{'color': 'red', 'edges': 4, 'perimeter': 20}

#Accessing values through keys
print(dict1['shape'])

#You can also use get, which doesn't cause an exception when the key is not found
dict1.get('false_key') #returns None
dict1.get('false_key', "key not found") #returns the custom message that you wrote 

#Deleting pairs
dict1.pop('volume')

#Merging two dictionaries
dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
dict1 #{'color': 'red', 'shape': 'square', 'area': 25, 'edges': 4, 'perimeter': 20}

#Getting only the values, keys or both (can be used in loops)
dict1.values() #dict_values(['red', 'square', 25, 4, 20])
dict1.keys() #dict_keys(['color', 'shape', 'area', 'edges', 'perimeter'])
dict1.items() 
#dict_items([('color', 'red'), ('shape', 'square'), ('area', 25), ('edges', 4), ('perimeter', 20)])
35
#title			:Dictionary Example
#author         :Josh Cogburn
#date           :20191127
#github         :https://github.com/josh-cogburn
#====================================================

thisdict = {
	"brand": "Ford",
 	"model": "Mustang",
 	"year": 1964
}

#Assigning a value
thisdict["year"] = 2018
29
# decleration
my_dict = {
  'spam': 'eggs',
  'foo': 4,
  100: 'bar',
  2: 0.5
}

# access single values from the dictionary
print(my_dict['spam']) # eggs
print(my_dict['foo']) # 4
print(my_dict[100]) # bar
print(my_dict[2]) # 0.5

# iterate over the dictionary
for key, value in my_dict.items():
  print(key, value)

# get length of the dictionary
print(len(my_dict)) # 4

# modify the dictionary
my_dict['baz'] = 'qux' # adds a pair
my_dict['baz'] = 'quxx' # also updates it
del my_dict['spam'] # removes a pair

# other methods
print(my_dict.copy()) # Returns a copy of the dictionary
print(my_dict.fromkeys('added', 100)) # Returns a dictionary with the specified keys and their values
print(my_dict.get('foo')) # Returns the value of the specified key
print(my_dict.items()) # Returns a list containing a tuple for each key value pair
print(my_dict.keys()) # Returns a list containing the dictionaries keys
print(my_dict.values()) # Returns a list of all the values in the dictionary
my_dict.setdefault('a', 'b') # Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
my_dict.pop('foo') # Removes the element with the specified key
my_dict.popitem() # Removes the last inserted key-value pair
my_dict.update({'baz': 'val'}) # Updates the dictionary with the specified key-value pairs
my_dict.clear() # Removes all the elements from the dictionary
9
# Dictionaries in Python

ages = {"John": 43, "Bob": 24, "Ruth": 76} # Marked by { at beginning and a } at end

# ^^^ Has sets of keys and values, like the 'John' and 43 set. These two values must be seperated by a colon

# ^^^ Sets of values seperated by commas.

2
shapes={"square": 90, "triangle": 60}
1
thisdictionary = {'key':'value','key1':'value1'}
print(thisdictionary['key'])
9

    x = dict(name = "John", age = 36, country = "Norway")
 
1

thisdict =	{

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

print(thisdict)

 
1
Dict = {"name": 'Izhaan', "salary": 1234, "age": 23} 
print("\nDictionary with the use of string Keys: ") 
print(Dict)
0

New to Communities?

Join the community