官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 张家界市| 寿宁县| 凤凰县| 蚌埠市| 太谷县| 铜川市| 错那县| 万宁市| 增城市| 如皋市| 融水| 称多县| 佛冈县| 芦溪县| 广州市| 泗阳县| 绥德县| 云阳县| 兴义市| 缙云县| 定陶县| 通辽市| 沙河市| 交城县| 塘沽区| 根河市| 阿尔山市| 公主岭市| 鸡西市| 德化县| 怀远县| 固安县| 翼城县| 固镇县| 开封市| 姚安县| 瑞丽市| 长寿区| 罗甸县| 孟津县| 察哈|