Day05:python List Method

Source: Internet
Author: User

1, list.append (obj) adds a new object at the end of the list and adds only one element at a time.

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.append([24, 23])>>> list[123, ‘kobego‘, [1, 2, 3], (1, 2), [24, 23]]

2. List.extend (seq) You can add multiple list elements at the end of the list at once.

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.extend([24, 23])>>> list[123, ‘kobego‘, [1, 2, 3], (1, 2), 24, 23]

3, List.pop (obj=list[-1]) removes an element from the list (the default is the end element) and returns the value of the removed element.

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.pop()(1, 2)>>> list[123, ‘kobego‘, [1, 2, 3]]

4, List.remove (obj) removes the first occurrence of an element in the list without a return value.

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.remove(123)>>> list[‘kobego‘, 123, [1, 2, 3], (1, 2)]

5. List.index (obj) finds the index position of the first occurrence of a value from the list, and the error is not found.

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.index(123)0

6, List.insert (index, obj) inserts the specified object into the specified position in the list.

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.insert(1, ‘kobego24‘)>>> list[123, ‘kobego24‘, ‘kobego‘, 123, [1, 2, 3], (1, 2)]

7. List.reverse () Reverses the order of the elements in the list.

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.reverse()>>> list[(1, 2), [1, 2, 3], 123, ‘kobego‘, 123]

8, List.sort ([func]) to sort the original list.

>>> list = [‘james‘, ‘kobego‘, ‘curry‘]>>> list.sort()>>> list[‘curry‘, ‘james‘, ‘kobego‘]

Day05:python List Method

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.