পাইথনে সেটের মত ডিকশনারি ডাটা টাইপও একটি স্পেশাল ডাটা স্ট্রাকচার যেটি অন্যান্য প্রোগ্রামিং ভাষাতে দেখে যায়না। এখানে ডাটাগুলো একটি নির্দিষ্ট key এবং value অনুসারে সজ্জিত থাকে। key দিয়ে value কে অ্যাক্সেস করা হয়। ডিকশনারিকে দ্বিতীয় বন্ধনীর মধ্যে লেখা হয়।
1 2 3 4 5 6 7 8 | # This program is an example of a dictionary in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict print (clothDict) |
ফলাফল-
{'name': 'Shirt', 'size': 'Long', 'type': 'Casual'}
নির্দিষ্ট কোন উপাদানকে অ্যাক্সেস (Access) করা
ডিকশনারিতে নির্দিষ্ট কোন উপাদানের ভ্যালুকে অ্যাক্সেস করার জন্য কি (key) এর প্রয়োজন পড়ে।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of a dictionary in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputDict = clothDict[ "name" ] print (outputDict) |
ফলাফল-
Shirt
একইভাবে কোন একটি ভ্যালু পরিবর্তনের জন্য Key এর মাধ্যমে করতে হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of a dictionary in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict clothDict[ "size" ] = "small" print (clothDict) |
ফলাফল-
{'type': 'Casual', 'size': 'small', 'name': 'Shirt'}
ডিকশনারিতে লুপ
ডিকশনারিতে লুপ চালিয়ে মূলত key গুলো পাওয়া যায়।
1 2 3 4 5 6 7 8 9 | # This program is an example of loop in a dictionary in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict for i in clothDict: print (i) |
ফলাফল-
name
type
size
যেহেতু লুপের মাধ্যমে key পাওয়া যায় তাই পূর্বের মত একইভাবে value ও বের করা যায়।
1 2 3 4 5 6 7 8 9 | # This program is an example of a loop in a dictionary in python clothDict={"name":"Shirt", "size":"Long", "type":"Casual" } # Defining a dict for i in clothDict: print(clothDict[i]) |
ফলাফল-
Shirt
Casual
Long
ডিকশনারি মেথডস (Dictionary methods)
ডিকশনারিতে বেশ কিছু বিল্ট-ইন মেথড আছে, সেগুলো দেখে নেই (বর্ণমালার ক্রমানুসারে) –
clear()
এই মেথডটি ডিকশনারিতে বিদ্যমান সকল উপাদানকে মুছে ফেলে।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary clear() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict clothDict.clear() # Clear() classmethod print (clothDict) |
ফলাফল-
{}
copy()
একটি ডিকশনারিকে অন্য একটি ডিকশনারিতে কপি করার জন্য এই মেথডটি ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary copy() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputDict = clothDict.copy() # copy() classmethod print (outputDict) |
ফলাফল-
{'name': 'Shirt', 'type': 'Casual', 'size': 'Long'}
del()
ডিকশনারির কোন একটি নির্দিষ্ট উপাদানকে মুছে ফেলার জন্য এই মেথডটি ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary del() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict del clothDict[ "size" ] # del() method print (clothDict) |
ফলাফল-
{'name': 'Shirt', 'type': 'Casual'}
সম্পূর্ণ ডিকশনারিটি মুছে ফেলতেও এই del() মেথডটি ব্যবহার করা যায়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary del() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict del clothDict # del() method print (clothDict) |
ফলাফল-
Traceback (most recent call last):
File "main.py", line 10, in <module>
print(clothDict)
NameError: name 'clothDict' is not defined
get()
এই মেথডটি কোন ডিকশনারির একটি নির্দিষ্ট উপাদানকে অ্যাক্সেস করার জন্য ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary get() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputValue = clothDict.get( "name" ) # get() classmethod print (outputValue) |
ফলাফল-
Shirt
keys()
এই মেথডটি ডিকশনারির সমস্ত কি (key) গুলোকে প্রদর্শন করে।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary keys() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputValue = clothDict.keys() # keys() classmethod print (outputValue) |
ফলাফল-
dict_keys(['type', 'size', 'name'])
len()
কোন একটি ডিকশনারিতে কতগুলো উপদান আছে সেটা জানার জন্য len() মেথডটি ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary len() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict dictLength = len (clothDict) # len() method print (dictLength) |
ফলাফল-
3
pop()
pop() মেথড ডিকশনারির একটি নির্দিষ্ট উপাদানকে মুছে ফেলতে ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 11 | # This program is an example of the dictionary pop() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputValue = clothDict.pop( "type" ) # pop() method print (outputValue) # popped value print (clothDict) |
ফলাফল-
Casual
{'name': 'Shirt', 'size': 'Long'}
popitem()
popitem() মেথডটি কোন একটা উপাদানকে দৈবচয়ন পদ্ধতিতে মুছে ফেলে।
1 2 3 4 5 6 7 8 9 10 11 | # This program is an example of dictionary popitem() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputValue = clothDict.popitem() # popitem() method print (outputValue) # popped value print (clothDict) |
ফলাফল-
('size', 'Long')
{'type': 'Casual', 'name': 'Shirt'}
setdefault()
setdefault() মেথডটি একটি ডিফল্ট ভ্যালু ঠিক করে রাখে। যদি কোন ডিকশনারিতে ওই নির্দিষ্ট key টি থাকে তাহলে ঐ ডিকশনারির ভ্যালুটিই গণ্য হবে, অন্যথায় ডিফল্ট ভ্যালু গণ্য হবে।
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # This program is an example of dictionary setdefault() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputValue = clothDict.setdefault( "size" , "short" ) # setdefault() method print (outputValue) print (clothDict) outputValue2 = clothDict.setdefault( "color" , "white" ) # setdefault() method print (outputValue2) print (clothDict) |
ফলাফল-
Long
{'size': 'Long', 'type': 'Casual', 'name': 'Shirt'}
white
{'size': 'Long', 'color': 'white', 'type': 'Casual', 'name': 'Shirt'}
update()
ডিকশনারিতে কোন ভ্যালু যুক্ত করার জন্য update() মেথডটি ব্যবহার করা হয়।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary update() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict clothDict.update({ "color" : "white" }) # update() method print (clothDict) |
ফলাফল-
{'type': 'Casual', 'name': 'Shirt', 'color': 'white', 'size': 'Long'}
values()
এই মেথডটি কিছুটা keys() মেথডের মত। values() মেথডটি কোন ডিকশনারির সকল value গুলোকে আলাদা করে।
1 2 3 4 5 6 7 8 9 10 | # This program is an example of dictionary values() method in python clothDict = { "name" : "Shirt" , "size" : "Long" , "type" : "Casual" } # Defining a dict outputDict = clothDict.values() # values() method print (outputDict) |
ফলাফল-
dict_values(['Long', 'Shirt', 'Casual'])