Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Download 1.25 Mb.
Pdf ko'rish
bet13/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   9   10   11   12   13   14   15   16   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Remove Method
Another method to remove items from a list by using the remove method.
Many times you do not know the specific position of a value that you want to


remove. In that case, you can remove the item by using the value of that item.
To do that you have to fill in the parenthesis of the remove() method with the
value.
officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table
lights']
print(officeitems)
officeitems.remove('scanner')
print(officeitems)
officeitems.remove('table')
print(officeitems)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights']
['printer', 'fan', 'table', 'chair', 'computer system', 'table lights']
['printer', 'fan', 'chair', 'computer system', 'table lights']
>>>
Just like you used the popped value later on in the code, you can also use the
removed value from the item.
officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table
lights']
print(officeitems)
removeditem = 'scanner'
officeitems.remove(removeditem)
print("I have sold the " + removeditem.title() + " because it was not adding
the desired value to the office.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights']
I have sold the Scanner because it was not adding the desired value to the
office.


>>>
List Organization
More often when you create lists, they flow in a kind of unpredictable order.
You cannot always control the order in which the users provide data to the
program. However, you can bring that information into perfect order later on.
It may happen more often than you want to make the information presentable.
This is the reason you should bring it into perfect order. There are several
ways by which you can order lists in Python.

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   9   10   11   12   13   14   15   16   ...   48




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