There are three built-in data structures in Python-lists, tuples, and dictionaries. We will learn how to use them, and how they can make programming easier.
List
A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list. Imagine you have a shopping list that says what you want to buy, and you can easily understand the list. Just on your shopping list, it's possible that everything is on its own, and in Python, you're separating each item with a comma. The items in the list should be enclosed in square brackets so that Python knows that you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type , that is, this type can be changed.
Working with lists
#!/usr/bin/python# filename: using_list.py# this is my shopping listshoplist = [' apple ', ' mango ', ' carrot ', ' banana ']print ' I have ', Len (shoplist), ' items to purchase. ' print ' These items are: ', # notice the comma at end of the linefor item in shoplist: print item, print ' \ni also have to buy rice. ' Shoplist.append (' rice ') print ' my shopping list is now ', shoplistprint ' I will sort my list now ' Shoplist.sort () print ' sorted shopping list is ', shoplistprint ' the first item i will buy is ', shoplist [0]olditem = shoplist[0]del shoplist[0]print ' i bought the ', olditemprint ' My shopping list is now ', shoPlist
How it works
The variable shoplist is a list of someone's shopping.
In Shoplist, we only store the name strings of the things you buy, but you can add any kind of objects including numbers or even other lists to the list.
Used for: In loops recursively between items in the list.
The list is a sequence.
Note that we used a comma at the end of the print statement to eliminate the line breaks that are automatically printed for each print statement.
Next, we used the Append method to add an item to the list, and then we checked to see if the item was actually added to the list by printing the contents of the list. The Print list simply passes the list to the print statement, and we can get a neat output.
Next, we use the Sort method of the list to sort the list. It should be understood that this method affects the list itself rather than returning a modified list-this differs from how the string works.
This is what we call the list to be mutable and the string immutable .
Finally, but when we finished buying something in the market, we wanted to remove it from the list. We use the DEL statement to do the work. Here, we point out which of the items in the list we want to delete, and the Del statement removes it from the list for us. We indicate that we want to delete the first element in the list, so we use del shoplist[0] (remember, Python counts from 0).
This article is from the "Thousand Face" blog, please make sure to keep this source http://oslibo.blog.51cto.com/10854638/1713426
Python Data Structure-list