After discussing list and tuple data types in the previous article, there is another array data type in Python, namely dictionaries. In a list or tuple, the index/key of the array does not need to be specified, while in the dictionary we have to specify it ourselves. Each key is separated by its value using a “:” sign. Like lists and tuples, each item in a dictionary is separated by a comma, starting and ending with curly braces.
Each key must be unique in one dictionary while the values are independent. Value can contain anything, while key must be a string or number. Below is an example dictionary:
#!/usr/bin/python #membuat dictionary biodata = {"Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; #menampilkan nama print "Nama : ", biodata['Nama']; #menampilkan no urut print "No Urut: ", biodata['NoUrut'];
So, according to the example above displays the value based on the key from the dictionary.
Unlike tuples, the data in the dictionary can be updated:
#!/usr/bin/python #membuat dictionary biodata = {'Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; #menambahkan data biodata['JenisKelamin'] = 'Pria'; #menampilkan data baru print "Jenis Kelamin: ", biodata['JenisKelamin'];
We can also delete data in the dictionary, there are 3 scripts that we can use as needed:
del biodata['Nama']; # menghapus key Nama biodata.clear(); # menghapus semua key pada dictionary del biodata; # menghapus dictionary
Multiple functions in dictionary
Function | Explanation |
cmp(d1,d2) | Comparing dictionaries d1 and d2 |
only (biodata) | Displays the number of items from the dictionary |
str (biodata) | Returns a string representation of a dictionary |
An example of using a dictionary function:
#!/usr/bin/python #membuat dictionary biodata1 = {'Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; biodata2 = {'Nama': 'Made', 'Asal': 'Denpasar', 'Umur': 23, 'NoUrut': 2}; biodata3 = {'Nama': 'Nyoman', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 3}; biodata4 = {'Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; #membandingkan dictionary print "Hasil perbandingan biodata1 dan biodata2 : %d" % cmp (biodata1, biodata2) print "Hasil perbandingan biodata1 dan biodata3 : %d" % cmp (biodata1, biodata3) print "Hasil perbandingan biodata1 dan biodata4 : %d" % cmp (biodata1, biodata4) #mendapatkan jumlah item dictionary print "Panjang biodata1 : %d" % len (biodata1) #menampilkan value dalam string print "String : %s" % str (biodata1)
Then there are methods that can be used in the dictionary
Method | Explanation |
biodata.clear() | Delete all keys in the biodata dictionary |
biodata.copy() | Copy the entire key in the biodata dictionary |
biodata.fromkeys() | Create a new dictionary with key based on dictonary biodata |
biodata.get() | Returns the value of the given key |
biodata.has_key() | Returns true if the key is found in the dictionary, and vice versa |
biodata.items() | Returns a dictionary item to a (key,value) tuple-like |
biodata.keys() | Get all the keys in the dictionary |
biodata.setdefault() | Same as get(), but can be used to set all values for each new key |
biodata1.update(biodata2) | Combining dictionary biodata2 with biodata1 |
biodata1.values() | Displays all values in dictionary |
Example of using the method above, I made a file:
#!/usr/bin/python #membuat dictionary biodata1 = {'Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; biodata2 = {'Nama': 'Made', 'Asal': 'Denpasar', 'Umur': 23, 'NoUrut': 2}; biodata3 = {'Nama': 'Nyoman', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 3}; biodata4 = {'Nama': 'Wayan', 'Asal': 'Gianyar', 'Umur': 21, 'NoUrut': 1}; temp = ('Nama','Asal','Umur','NoUrut'); biodataBaru = {'JenisKelamin' : 'pria' } #menghapus semua item dictionary biodata1 biodata1.clear() print "Jumlah setelah dihapus : %d" % len(biodata1) #meng-copy dictionary biodata4 ke biodata1 biodata1 = biodata4.copy() print "biodata1 : %s" % str(biodata1) #membuat dictionary baru dari temp biodata5 = biodata5.fromkeys(temp) print "Dictionary biodata5 : %s" % str(biodata5) #mengembalikan nilai dari key print "Value : %s" % biodata2.get('NoUrut') #mengecek key dari dictionary print "Value : %s" % biodata2.has_key('Nama') #merubah item menjadi list print "Value : %s" % biodata2.items() #menampilkan semua key print "Key : %s" % biodata3.keys() #menambah item dengan default value print "Item baru : %s" % biodata4.setdefault('Hobi', None) #mengupdate dictionary biodata4.update(biodataBaru) print "Update biodata4 : %s" % biodata4 #menampilkan semua value dari dictionary print "Semua value : %s" % biodata4.values()
Hope it’s useful
What is a dictionary in Python?
Dictionary is the data type on python which works to store groups of data/values with a “key-value” approach.
What is a dictionary data type?
Dictionary is data type in Python that works to store groups data or values, each of which contains a key and a value.
A data structure that looks like a dictionary has keywords and then there is a value. The keyword must be unique, while the value can be filled with anything. What is the data type?
Dictionary is a structure data that looks like a dictionary. There are keywords then there are the value. Keywords must be unique, while values can be filled with anything. In the example above we create a Dictionary named me with isi name data and URLs.
What are Python data structures?
Now we will study the data structure of List, Tuple, Set, and Dictionary..
1. Lists. A list is a data structure in the python programming language that is capable of storing a collection of data (objects/values), which are called list elements. The elements in the list are stored in a certain order. … .
2. Tuple. … .
3. Set..