Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Small methods in the list

Shulou Source: shulou.com Published: 2022-06-03 07:09:20 10月25日 Update

The methods involved in this blog are: index (), append (), insert (), remove (), sort ().

1.index (): accepts a value that returns its subscript if it is in the list, and only the first subscript if it occurs multiple times

List = ['hello','world','hi'] print (list.index (' hello')) print (list.index ('hi'))

The returned value is: 0prime1

2.append () inserts data at the end of the list, and insert (new value subscript, new value) means to insert data anywhere in the list.

Note: the return values of append () and insert () are all None

List = ['hello','world','hi'] list.append (' add to end') list.insert (10 'new value anywhere')

3.remove () accepts a value and removes it from the list

List = ['hello','world','hi'] list.remove (' hello')

4.sort () is used to sort lists of numeric values or strings. Strings are sorted by the ASCII value of the first letter

List = ['hello','world','Hi','a','A'] list.sort () # default ascending sequence print (list) list.sort (reverse = True) # descending sequence print (list) list.sort (key = str.lower) # in normal dictionary order (all items are considered lowercase) print (list)

Tags: Subscript position character string data end sort method normal Dictionary letter lowercase numeric order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi macOS Shulou Tech Info NVidia