Several methods of the most commonly used operations list in Python are summarized _python

Source: Internet
Author: User
Tags extend python list

Here are a few common list operations
adding elements

Adding elements using a built-in method for a list 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]]
Number.append ({' A ': ' B '}) # number = [1, 2, 3, 4, [6, 7], {' A ',: ' B '}

You can see a powerful Python list that can be nested in any type
Add list

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

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]

Using the + number will 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 methods of the dictionary type

A = [1, 2, 2, 3, 1, 3]
B = {}.fromkeys (a). Keys () # b = [1, 2, 3]

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.