The differences and usage of collection set, Dictionary dict and list lists in Python

Source: Internet
Author: User

In Python, set represents the collection, list stands for lists, dict represents the dictionary

The difference between set and Dict is that the dict is a storage key-value, each key is unique, the set is stored as a key relative to the dict, and the key is unique, and the list can be stored in addition to the variables.

Dict={"A": 1, "B": 2, "C": 3}

Set={"A", "B", "C"}

List=["A", "B," C "]

1.set usage

There are two common methods for adding Python collections, namely add and update.
Add method: The element to be passed into the collection as an entire, for example:
>>> a = set (' Boy ')
>>> a.add (' python ')


>>> A
Set ([' Y ', ' python ', ' B ', ' O '])

The Update method: Splits the element to be passed in as an individual into the collection, for example:
>>> a = set (' Boy ')
>>> a.update (' python ')
>>> A
Set ([' B ', ' H ', ' o ', ' n ', ' P ', ' t ', ' Y '])

Collection Delete Action method: remove
Set ([' Y ', ' python ', ' B ', ' O '])
>>> a.remove (' python ')
>>> A
Set ([' Y ', ' b ', ' O '])

Advanced usage of Set, set operation

2.list usage

Add append (e) to the list, append to the bottom

Remove Pop (index i) from the list to delete the element that contains the index

Removing the Remove (e) method from the list, deleting the element E

High-level usage of the list list.extend (LIST2) or a set of two lists added together with +

Difference: Extend () is the element that adds list2 to the list and does not create a new element

+ is to generate a new list

3.dict Dictionary Usage

add element dict[']= '

Delete Element pop

4.list and set convert each other

The differences and usage of collection set, Dictionary dict and list lists in Python

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.