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



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

Looping Through Values Only
In  the  same  manner  that  we  can  easily  loop  through  the  keys  in  a  given
dictionary, we can also loop through the value and display the value list. We
can use the values() method to achieve this feat. Remember, in the previous
example, we use the keys() method.
election_list = {
‘John Campbell’ : ‘Freedom Party,’
‘Eric Longman’: ‘Sovereign Nation Party,’
‘Amanda Drill’: ‘ Women Liberation Movement,’
‘Condolence Rice’: ‘Democratic Party,’
‘Donald Trump’: ‘Republican Party,’
}


print(“This is the list of Political Party for the election:”)
for party in election_list.values():
print(party.title())
The “for” statement pulls all the value in our dictionary list and assigns it to
the party. The print statement then prints all the values in the election_list.
This is the list of Political Party for the election:
Freedom Party
Sovereign Nation Party
Women Liberation Movement
Democratic Party
Republican Party
This  approach  prints  all  the  value  available  in  the  dictionary  without
verifying any repetition. Although this may work perfectly in a small list,
however, when you have to pull a large name or item, there may be room
for repetition. To avoid such repetition, there is a way out. We can use a set,
which is a group where every item is unique.
election_list = {
‘John Campbell’ : ‘Freedom Party,’
‘Eric Longman’: ‘Sovereign Nation Party,’
‘Amanda Drill’: ‘ Women Liberation Movement,’
‘Condolence Rice’: ‘Democratic Party,’
‘Donald Trump’: ‘Republican Party,’
‘Nicolas Fred’: ‘ Sovereign Nation Party,’
}
print(“The following Political Parties have been mentioned already:”)
for party in set(election_list.values()):
print(party.title())
We added another name and party to the list – Nicolas Fred. Assuming we
are printing only the value list and don’t want a repetition of the party. Once
you use the set() around the list, you don’t want duplicate; python identifies
this  as  a  unique  item  in  the  list.  In  the  end,  there  won’t  be  any  repeated
detail.
The following Political Parties have been mentioned already.
Freedom Party
Sovereign Nation Party
Women Liberation Movement


Democratic Party
Republican Party

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   69   70   71   72   73   74   75   76   ...   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