- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 251字
- 2021-07-02 21:09:31
List
The next data type we're going to look at is a list, which can be created with the list expression. A list expression is just a pair of square brackets surrounding the data values we want to store in the list, with each value separated by a comma. It is not necessary that each of the values be of the same type. The code example for a list is as follows:

In the preceding example, they're strings, but they could be numbers, or a list, or any other kind of data mixed together. We can use a lookup expression to retrieve data values.
Unlike with a dictionary though, the keys for a list are integers. That's because instead of associating key values with data values, a list just stores its data values in order. The key for the very first item in the list is 0. The key for the next item is 1, and so on. We can also use negative integers for the key. We still get a data value out, but it's counted from the end of the list instead of the beginning, with the item at -1 being the last item in the list.
We can use the list.append function to add a new item at the end of the list or its insert function to add a new item anywhere as shown in the following code:

The list will automatically grow to be large enough to hold all of the data we put into it.
- OpenCV實例精解
- Visual FoxPro 程序設計
- Building Mapping Applications with QGIS
- Python編程:從入門到實踐
- Learning Salesforce Einstein
- Visual C++開發入行真功夫
- Python極簡講義:一本書入門數據分析與機器學習
- 西門子S7-200 SMART PLC編程從入門到實踐
- Advanced Express Web Application Development
- C++寶典
- Webpack實戰:入門、進階與調優(第2版)
- Android嵌入式系統程序開發(基于Cortex-A8)
- LabVIEW入門與實戰開發100例(第4版)
- 產品架構評估原理與方法
- 人件集:人性化的軟件開發