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


How to Remove Instances of Particular Values from a List



Download 2,02 Mb.
Pdf ko'rish
bet108/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   104   105   106   107   108   109   110   111   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

How to Remove Instances of Particular Values from a List
At times, you may decide to remove a particular value from a list. Well, you
remember  the  remove()  function  we  talked  about?  This  works  perfectly
well; however, what if we want to remove values that appear twice in a list.
That also is possible to do in Python.
For  instance,  here  is  a  list  of  footballers'  names  with  “Oscar”  repeated
thrice.  You  can  use  a  while  loop  to  remove  all  instances  of  the  name
pending when there is no “Oscar.”  Check the code to see as that is possible.
footballers  =  [“Oscar,”  “Messi,”  “Vardy,”  “Oscar,”  “Ronaldo,”  “Oscar,”
“Fred,” “Lampard,” “Terry”]
print(footballers)
while “Oscar” in footballers:
footballers.remove(“Oscar”)
print(footballers)
The logic of the program is very easy to understand. We assign the variable
footballers  with  various  value,  but  “Oscar”  repeated  thrice.  It  prints  the
names of the footballers. Then the code enters the while loop because we


have Oscar in our list in more than one instance. Inside the loop, the first
instance  of  Oscar  returns  to  the  while  line  before  it  reenters  the  loop  to
ascertain if there is another Oscar. It continues in such a manner until the
list is empty. The output for our program will be”:
[“Oscar,”  “Messi,”  “Vardy,”  “Oscar,”  “Ronaldo,”  “Oscar,”  “Fred,”
“Lampard,” “Terry”]
[“Messi,” “Vardy,” “Ronaldo,” “Fred,” “Lampard,” “Terry”]

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   104   105   106   107   108   109   110   111   ...   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