Python next day-list basic features

Source: Internet
Author: User

1. How to define alist
arr=["ABC","def","ghi"  ]
2.Append Method-Append elements to the tail of the list
arr=["ABC","def","ghi"] Arr.append ("jkl")print(arr);

The results are:[' abc ', ' Def ', ' Ghi ', ' JKL ']

3. Clear Method-Clears all data in the list
arr=["ABC","def","ghi"] Arr.clear ()print(arr);

The result is: []

4. Copy Method-copies all the elements in the list to the other list
arr=["ABC","def","ghi"] ARR2=arr.copy ()print(ARR2);

The result:[' abc ', ' Def ', ' Ghi ']

5.count("") Method--count the number of occurrences of the specified element in the list
arr=["ABC","def","ABC"] Count=arr.count ("ABC")print(count);

The result is: 2

6.Extend() appends all elements of a list to the specified list tail
arr=["ABC","def","ABC"] List2=["123","456","789  "]arr.extend (list2)print(arr);

The results are:[' abc ', ' Def ', ' abc ', ' 123 ', ' 456 ', ' 789 ']

7.index()--Gets the position of the first occurrence of the specified element in the list
arr=["ABC","def","ABC"  ]index=arr.index ("ABC")print(index);

The result is:0

8.Insert ()--Specifies the position of the subscript specified in the list to insert the element
arr=["ABC","def","ABC"] Arr.insert (0," hello ")print(arr);

The result is:[' Hello ', ' abc ', ' Def ', ' abc ']

9.Pop() removes the last element from the specified list
arr=["ABC","def","ABC"] Arr.pop (); Print (arr);

The result is:[' abc ', ' Def ']

Remove ()--Removes the element that is the first occurrence of the specified element in the list from left to right
arr=["ABC","def","ABC"] Arr.remove ("ABC"); Print (arr);

The result is:[' def ', ' abc ']

Reverse()--will specify the list flip order
arr=["1","2","3"] Arr.reverse (); Print (arr);

The result is:[' 3 ', ' 2 ', ' 1 ']

sort ()--Sorts the specified list (if the list appears with both letters and numbers, it throws an exception)
arr=["3","1","2","4","9","7","8","5","0"]arr.sort ();Print(arr);

The results are:[' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 7 ', ' 8 ', ' 9 ']

The .del keyword: You can delete an element of the list specified subscript range
 arr=[ " a   ", "  b   ", "  c   ", "  d   ", "  e   ", "  f   ", "  g   " ]  del  arr[0:3  " print  (arr); 

The result is:[' d ', ' e ', ' f ', ' G ']

--------------------------------------End------------------------------------------------------------

The next day to learn python, energetic or very abundant, hope to continue to anger, so far away from the great god of Python!

Go on with your studies, don't blow.

Python next day-list basic features

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.