Python Training Knowledge Summary Series-chapter II Python data Structure Part III-dictionaries, collections

Source: Internet
Author: User

Write a function remove_duplicates that takes the list as an argument and returns a new list that contains the unique elements in the source list. Elements that do not recur in the new list can take any order.
Target=[]
def remove_duplicates (list):
For each in list:
If each isn't in target:
Target.append (each)
return target

A collection is a collection of unique elements, without any particular sort. We can create a collection from a list like this:
Country_ set = set (countries)
Len (Country_set)
196

Adding elements to a collection
Country_set.add ("Florin")

The collection also has a pop method that is the same as the list. A random element is removed from the collection of pop elements (remember that the collection differs from the list and is unordered, so there is no "last element".)

。 Dictionaries do not store individual objects like lists and collections, but instead store element pairs: Key (key) and values (value). In this example, we define a dictionary where the keys are the element names, and the values are their atomic number.

elements = {' hydrogen ': 1, ' Helium ': 2, ' Carbon ': 6}
You can find the values in the dictionary by enclosing the key in square brackets:

Print (elements[' carbon ')
6
You can also insert new values into the dictionary using square brackets:

elements[' lithium '] = 3
Print (elements[' lithium ')
3

Python Training Knowledge Summary Series-chapter II Python data Structure Part III-dictionaries, collections

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.