The difference between a list tuple dictionary collection is one of the most common problems in Python interviewing. Although the problem is very basic, it does reflect the basic level of the participants.
(1) List
What is a list? I think lists are a list that we often see in our daily lives. For example, to count the things we've bought in the past week, list them. Since we can buy one thing more than once, there are duplicates allowed in the list. If we expand the scope of the list and count what we have spent the past week, then this is also a list, but there are different categories of items in the list, such as when we buy things as a kind of cost, and the electricity bills are also a kind of expense, and the types of these items can be different. The list of Python is the same as the list, characterized by: repeatable, different types. Different types are the most essential differences from arrays. The list in Python is denoted by "[]":
LST = [' Arwen ', 123]
Print Lst[0]
Print Lst[1]
Lst[0] = ' Weiwen '
There are two ways to add items to a list: Append and extend. Append is to
Such as:
(2) Meta-group
Tuples and lists do not differ structurally, the only difference is that tuples are read-only and cannot be modified. Tuples are denoted by "()", such as:
Tup = (' Arwen ', 123)
Print Tup[0]
Print Tup[1]
(3) The set is the set of our mathematics, and there is no special definition. The best application for a collection is to go heavy. A collection does not have a special representation, but instead is transformed into a set by a set function, such as:
LST = [1, 1, 0]
Lst_set = Set (LST) #lst_set is 1, 0
Tup = (2, 2, 1)
Tup_set = Set (tup) # Tup_set is 2, 1
For item in Lst_set:
Print Item
(4) The last one is a dictionary. The dictionary stores key-value pairs of data, such as:
1:a,2:b,3:c
The outside of the dictionary is curly braces, each group is linked by a colon, and the groups are separated by commas.
The maximum value of a dictionary is the query, through the key, to find the value.
Differences in the list tuple dictionary collection in Python