List in Python 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.