Python The following data structures are supported : list , Dictionaries , Tuples , aggregate .
When to use a dictionary :
When you need keys : When the logical association between value pairs .
When you need to quickly find data based on a custom key .
When your data is constantly modified . please remember , The dictionary is changeable .
When to use other types :
If you have some data sets that don't need random access , Please use the list . When you need a simple , Iteratively, frequently modified sets can use lists .
If you need element uniqueness , Use set .
Use tuples when data cannot be changed .
A lot of times , Tuples are used in conjunction with dictionaries , For example, tuples may represent a keyword , Because it's immutable .
Use square brackets establish
words = ["Hello", "world", "!"]
Create an empty list with empty square brackets
It can be accessed by index
Most of the time , The last item in the list does not have a comma . However , Placing a comma on the last item is completely valid , In some cases, it is encouraged .
The index of the list is from 0 At the beginning , Not from 1 At the beginning
Use Curly braces or set Function creation
num_set = {
1, 2, 3, 4, 5}
word_set = set(["spam", "eggs", "sausage"])
To create an empty set , You have to use set(), Such as {} Is to create an empty dictionary .
A set is disorder Of , This means they cannot be indexed .
Collection cannot contain duplicate elements .
Due to the way of storage , Checking whether an item is part of a set is better than checking whether it is part of a list faster .
Use together add Additive elements .
remove Method to delete a specific element from the collection ; pop Delete random elements .
Tuples Use parentheses establish , Can also be in the absence of parentheses Created in case of
words = ("spam", "eggs", "sausages",)
my_tuple = "one", "two", "three"
Create an empty tuple with empty bracket pairs .
Tuples are faster than lists , But tuples cannot be changed .
You can use indexes to access values in tuples .
A dictionary is a data structure used to map any key to a value
ages = {
"Dave": 24, "Mary": 42, "John": 58}
An empty dictionary is defined as {}.
Dictionaries Each element in the consists of a key : value Yes, to show .
Use Dictionaries [“ Key name ”] You can get the corresponding value .