যদি ডিকশনারির লিস্টকে কি-এর 'i'-তম ইনডেক্স ভ্যালু অনুযায়ী সর্ট করতে হয়, তবে 'sorted' মেথড এবং lambda মেথড ব্যবহার করা হয়।
উদাহরণ
নিচে এর демонস্ট্রেশন দেওয়া হলো:
my_list = [{"Python" : "Best", "to" : "Code"}, {"Python" : "Good", "to" : "Learn"}, {"Python" : "object", "to" : "cool"}, {"Python" : "oriented", "to" : "language"}] print("The list is : " ) print(my_list) K = "Python" print("The value of K is ") print(K) i = 2 print("The value of i is :") print(i) my_result = sorted(my_list, key = lambda sub: sub[K][i]) print("The resultant list is : ") print(my_result)
আউটপুট
The list is : [{'Python': 'Best', 'to': 'Code'}, {'Python': 'Good', 'to': 'Learn'}, {'Python': 'object', 'to': 'cool'}, {'Python': 'oriented', 'to': 'language'}] The value of K is Python The value of i is : 2 The resultant list is : [{'Python': 'oriented', 'to': 'language'}, {'Python': 'object', 'to': 'cool'}, {'Python': 'Good', 'to': 'Learn'}, {'Python': 'Best', 'to': 'Code'}]
ব্যাখ্যা
একটি ডিকশনারির লিস্ট তৈরি করা হয় এবং কনসোলে দেখানো হয়।
'K' এর মান ডিফাইন করা হয় এবং কনসোলে দেখানো হয়।
'i' এর মান ডিফাইন করা হয় এবং কনসোলে দেখানো হয়।
'sorted' মেথড ব্যবহার করা হয় লিস্ট সর্ট করতে যেখানে ল্যাম্বডা ফাংশন কি হিসেবে ব্যবহার করা হয়।
এটি একটি ভেরিয়েবলে অ্যাসাইন করা হয়।
এই ভেরিয়েবল আউটপুট হিসেবে কনসোলে দেখানো হয়।