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