Several ways to summarize the most commonly used operations list in Python

Source: Internet
Author: User
Here are a few common list operations
adding elements

Add elements using a list of built-in methods append

Number = [1, 2, 3, 4]number.append (5) # number = [1, 2, 3, 4, 5]number.append ([6,7]) # number = [1, 2, 3, 4, 5, [6, 7]]num Ber.append ({' A ': ' B '}) # number = [1, 2, 3, 4, [6, 7], {' A ',: ' B '}

You can see a powerful Python list that can nest any type
List additions

To connect to two lists, you can connect using the + sign

A = [1, 2, 3]b = [4, 5, 6]c = a + b # c = [1, 2, 3, 4, 5, 6]

You can also use the list built-in method extend to connect two lists

A = [1, 2, 3]b = [4, 5, 6]a.extend (b) # a = [1, 2, 3, 4, 5, 6]

Use the + sign to create a new pass object, using extend to modify the original object
List to repeat

The list itself does not remove duplicate functionality, but it can be viewed with another type of Python set (help set)

A = [1, 2, 3, 3,2, 1]b = List (set (a)) # b = [1, 2, 3]

You can also use the built-in method of the dictionary type

A = [1, 2, 2, 3, 1, 3]b = {}.fromkeys (a). Keys () # b = [1, 2, 3]
  • 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.