Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
List Slicing
You can slice a list to work with a portion of a list. This is helpful if you are building a long list of items and you have to work only with a handful of items. I will use the same list for slicing. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] print(officeitems[1:4]) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['scanner', 'fan', 'table'] >>> The result contains only the items that I have sliced out of the original list. The output comes in the form of the original structure of the list. The sliced part of the list can also be named as a subset of the list. If you omit the index's start, the subset of the list will start from the first item. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] print(officeitems[:4]) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['printer', 'scanner', 'fan', 'table'] >>> Similarly, you can omit the second half of the range index. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] print(officeitems[2:]) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['fan', 'table', 'table lights'] >>> The negative indexing is also available for Python lists. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] print(officeitems[-2:]) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['table', 'table lights'] >>> You also can loop through the subset of a list or the slice of a list just like we did with a complete list. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] print("Here are the items that have cost double the value of others.") for officeitem in officeitems[:4]: print(officeitem.title()) = RESTART: C:/Users/saifia computers/Desktop/sample.py Here are the items that have cost double the value of others. Printer Scanner Fan Table >>> Copying You can create a copy of the list based on the original list. This is helpful if you have packed up customer data in a list and you want to save it elsewhere to secure it in the wake of data breaches or any other kind of cyberattack. The most common way of making a copy of the list is to create a slice with no starting or ending values. This slice instructs Python to slice the list right from the starting value to the ending value. This is how you end up creating a copy of your list. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] c_officeitems = officeitems[:] print("Here is the list of items that I have purchased.") print(officeitems) print("\nHere is the exact copy of the same items.") print(c_officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py Here is the list of items that I have purchased. ['printer', 'scanner', 'fan', 'table', 'table lights'] Here is the exact copy of the same items. ['printer', 'scanner', 'fan', 'table', 'table lights'] >>> Let us now check if you really have two lists and that if removing or modifying one list will affect the copied list or not. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] c_officeitems = officeitems[:] officeitems.append('computer system') c_officeitems.append('blinds') print("Here is the list of items that I have purchased.") print(officeitems) print("\nHere is the new list of office items.") print(c_officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py Here is the list of items that I have purchased. ['printer', 'scanner', 'fan', 'table', 'table lights', 'computer system'] Here is the new list of office items. ['printer', 'scanner', 'fan', 'table', 'table lights', 'blinds'] >>> You can see that the two lists exist independently and that you can modify them separately. This is helpful if you are building data lists for a big financial institution. You may create a copy of the list without using the slice method but you will not be able to modify the two lists independently of each other. They will not be able to exist independently of each other. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] c_officeitems = officeitems officeitems.append('computer system') c_officeitems.append('blinds') print("Here is the list of items that I have purchased.") print(officeitems) print("\nHere is the new list of office items.") print(c_officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py Here is the list of items that I have purchased. ['printer', 'scanner', 'fan', 'table', 'table lights', 'computer system', 'blinds'] Here is the new list of office items. ['printer', 'scanner', 'fan', 'table', 'table lights', 'computer system', 'blinds'] >>> You can see that each list retained both newly added items. I will now add one item to one list only and see if the copied list retains that item or not. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] c_officeitems = officeitems officeitems.append('computer system') print("Here is the list of items that I have purchased.") print(officeitems) print("\nHere is the new list of office items.") print(c_officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py Here is the list of items that I have purchased. ['printer', 'scanner', 'fan', 'table', 'table lights', 'computer system'] Here is the new list of office items. ['printer', 'scanner', 'fan', 'table', 'table lights', 'computer system'] >>> So, as you can see that simply copying a list will not do the magic for you. Slicing helps you make the copy you desire for. Download 1.25 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling