1. Get a single element from the list
You can get a single element from the list by the index value of the element, starting with the index value 0.
>>> nums = [' Zhao ', ' money ', ' sun ', ' Li ', ' Zhou ']>>> nums[0] ' Zhao '
2. Get multiple elements from the list
>>> nums = [' Zhao ', ' money ', ' Sun ', ' Lee ', ' Week ']>>> nums[1:3] #表示获取索引值从1 element, excluding 3[' money ', ' Sun ']>>> nums[ : 3] #冒号左边为空表示从列表第一个元素开始获取 [' Zhao ', ' money ', ' Sun ']>>> nums[:] #两边为空表示获取整个列表的元素 (from the beginning to the end) [' Zhao ', ' money ', ' Sun ', ' Lee ', ' Zhou ‘]
3. Remove an element from the list
① removes the element from the list with the Remove command, the value of the element when the parameter is required
>>> nums = [' Zhao ', ' money ', ' Sun ', ' Lee ', ' Week ']>>> nums.remove (' money ') >>> nums[' Zhao ', ' sun ', ' Li ', ' Week ']
② removes an element from the list via the DEL statement, with the following syntax: Del list name [index value]
>>> nums =[' Zhao ', ' sun ', ' Li ', ' Week ']
>>> del Nums[0]
>>> Nums
[' Zhao ', ' Li ', ' Week ']
③ removes the element from the list by pop and returns the value of the element.
Pop Add parameter You can delete the element that makes the index value in the list, and the last element of the list is deleted by default when the parameter is empty.
>>> nums = [' Zhao ', ' money ', ' Sun ', ' Lee ', ' Week ']>>> nums.pop () #pop参数为空时默认删除列表最后一个元素 ' Week '
>>> Nums
[' Zhao ', ' money ', ' sun ', ' Li ']
>>> nums.pop (0) #填写了参数时则删除指定索引值的元素
Zhao
>>> Nums
[' Money ', ' sun ', ' Li ']
List in Python 2