Python list additions and deletions

Source: Internet
Author: User
Tags python list

List additions and deletions
First, Increase: append (), insert () method
1.append () Method: Add an element at the end
#szz = [' ab ', ' jhj ', ' nhy ']
#szz. Append (' msr ')
2.insert () Method: Add elements or lists at the specified location
#szz = [' ab ', ' jhj ', ' nhy ']
#szz1 = [' msr ', ' Mike ']
#szz. Insert (0, ' Mark ')
#szz. Insert (0,SZZ1)
Second, delete: Del, pop (), remove (), clear ()
1.del: You can delete the specified value of the underlying

#del Szz[1] #删除下标为1的值
2.pop () method:#默认从列表的末尾删除, you can also delete the specified value

Szz.pop ()

The query list can be labeled and sliced
#2 (1) Subscript value, starting from 0
#2 (2) Slice: Gu Tou regardless of tail, and the slice subscript operation is also used for string
"""
names = [' Mike ', ' Mark ', ' Candice ', ' Laular '] #定义列表
info = ' ABCDEFGHIJKLMN '
Num_list = List (range (1,10)) #range生成一个长度为10的
Print (names[2]) #读取列表的值
Print (Names[1:3]) #通过切片方式取值, the slice is Gu Tou regardless of the tail, printing results: [' Mark ', ' Candice ']
Print (names[1:]) #取下标后面所有的值, printing results: [' Mark ', ' Candice ', ' Laular ']
Print (Names[:3]) #取下标前面所有的值, printing results: [' Mike ', ' Mark ', ' Candice ']
Print (names[:]) #取所有的值, printing results: [' Mike ', ' Mark ', ' Candice ', ' Laular ']
Print (Names[-1]) #取最后一个值, printing results: laular
Print (Names[:1:2]) #隔几位取一次, the default is not to write a step of 1, that is, take one step at a time, the result is to remove the value before the Mark 1, take a [' Mike '] 2 bits
Print (Num_list[::-1]) #从后往前取值 with a step size of 1
Print (num_list)
Print (Info[1:7])
"""



#修改
#szz [0] = ' zzj '
#print (SZZ) #打印结果: [' zzj ', ' ab ', ' jhj ', ' nhy ', ' zxf ']
#删除
#del
#del Szz[1] #删除指定下标的值
#print (SZZ)
#内置方法pop ()
#szz. Pop ()
#print (SZZ)
#szz. Pop (2)

#remove, delete the specified value instead of the subscript
#szz. Remove (' jhj ')
#print (SZZ)
#clear () #清空
#szz. Clear ()
#index, gets the subscript for the specified element
#szz. Index (' jhj ')
#count, gets the number of times the specified element
#szz. Count (' MSR ')
#extend, merge the list, modify the value of the original list
#szz. Extend (SZZ1)
#sort排序, by default ascending from small to large row, descending row add: reverse=true, reverse return meaning
"" "#sort只是一个方法, no return value, so cannot print
Numbs = [12,425,334,32]
Numbs.sort (Reverse=true)
Print (numbs)
"""
#元组的值, can not be changed, is an unreachable list, want to change must first turn to list
#元祖只有count和index方法
Names = (' Mike ', ' Mark ', ' Candice ', ' laular ', ' Mark ', ' msr ')
Print (Names.count (' Mark '))
Print (Names.index (' msr '))


#3. Dictionaries:


Python list additions and deletions

Related Article

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.