Learning Python: The Ultimate Guide for Beginners to Coding with Python with Useful Tools


How to Use a List Inside a Dictionary



Download 2,02 Mb.
Pdf ko'rish
bet75/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   71   72   73   74   75   76   77   78   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

How to Use a List Inside a Dictionary
In our previous examples, our dictionary is inside a list; however, we can
also  use  a  list  inside  our  dictionary  depending  on  what  you  want.  For
instance,  you  may  decide  to  react  to  a  particular  way  to  describe  when


ordering a meal. If you use a list, all you need is to store the meals. In the
program below, we only stored two-piece of information for each meal
meal = {
‘food’ : ‘ beans,’
‘drink’: [‘Fanta,’ ‘Water’],
}
#summary of the order
print(f “You ordered {meal [‘food’]} with the following drink:”]
for drink in meal[‘drink’]:
print(“\t” + drink)
We  begin  by  creating  a  dictionary  “meal”  to  hold  information  about  the
food you ordered. Remember, every list has a key-value pair, respectively.
In  this  case,  the  key  for  food  is  beans,  whereas  that  of  the  drink  includes
Fanta and Water. Then we summarize our order before creating the meal. In
order to access our drink list, we use the “for loop” to print the drink list.
Let us see how the output for this program will be:
You ordered beans with the following drinks:
Fanta
Water
Easily done in such a way that you can understand each line. Besides this,
you  can  also  nest  your  list  in  a  dictionary  when  you  want  a  key  to  have
more  than  one  value.  Let  us  look  at  our  previous  election_list  program,
where  candidates  can  choose  two  political  parties.  Assuming  we  have  to
store their responses in a list, a particular person can belong to more than
one political party.

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   71   72   73   74   75   76   77   78   ...   126




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish