1. Comparison of data types
tuple |
the list |
gather |
dictionary |
|
dynamic |
Immutable |
variable |
variable |
variable |
sequence |
orderly |
orderly |
out of order |
out of order |
heterogeneous |
heterogeneous |
heterogeneous |
heterogeneous |
heterogeneous |
repeat |
repeatable |
repeatable |
not heavy |
not heavy |
1. Dynamic
Variable and immutable refer to whether the space in memory can be changed
When the data is added, deleted or modified, and the address of the data does not change, it is a variable type ;
When the data is added, deleted, or modified, and the address of the data changes, it is an immutable type . The computer will reallocate a piece of memory space.
Note: Dictionaries are mutable data types. But the key of the dictionary is immutable, only the value of the key can be changed or the entire key-value pair can be deleted. The keys of the dictionary can only use numbers, tuples and strings.
2. Sequence
Sequence: whether there is an order among the internal elements
Ordered sequences support indexing and slicing
3. Heterogeneity
Heterogeneity: can store elements of different types
Note: The elements in the dictionary are key-value pairs, and the heterogeneity of the dictionary is that the keys and values can be elements of different types.
4. Repeat
Repeat: there can be more than one of the same element
Note: The keys of the dictionary cannot be repeated, but the values can be repeated.
2. Comparison of common methods
tuple |
the list |
gather |
dictionary |
|
Establish |
tuple() |
list() |
set() |
dict() |
() |
[] |
{} |
{} |
|
list comprehension | set comprehension | dictionary comprehension | ||
increase |
/ |
append() |
add() |
dict[key]=value |
extend() |
update() |
update() |
||
insert() |
||||
delete |
/ |
pop() |
pop() |
pop() |
remove() |
remove()/discard() |
|||
clear() |
clear() |
clear() |
||
of the |
of the |
of the |
of the |
|
change |
/ |
list[index]=value |
dict[key]=value |
|
+ * |
+ * |
& | - |
||
check |
tuple[index] |
list[index] |
dict[key] |
|
get() |
||||
in/not in |
in/not in |
in/not in |
in/not in |
|
traverse |
traverse |
traverse |
traverse |
|
index() |
index() |
|||
count() |
count() |
|||
to sort |
sort() |
|||
reverse() |
1. Establish
- Tuples, lists, sets, and dictionaries can all be created using their respective constructors
- Tuples, lists, sets, and dictionaries can be created using (), [], {}, {} respectively (sets must be filled with curly braces to create an empty set, and empty curly braces are dictionaries Format)
- Lists, sets, and dictionaries can be created using comprehensions
2. increase
- List: append receives a single object, extend receives an iterable object, both of which are inserted at the end position; insert can specify the insertion position, and requires 2 parameters
- Collection: add receives a single object, update receives an iterable object
- Dictionary: update directly passes in the dictionary object. You can also use dict[key]=value to add elements
- The return value is None
3. Delete
pop()
- List: Delete the specified index element or the unspecified end element. IndexError is raised if the index value is incorrect or the list is empty
- Sets: Randomly delete an element. collection is empty, KeyError is raised
- Dictionary: delete the key-value pair of the specified key. key does not exist in the dictionary, raise KeyError error
- The return value is the value of the deleted element
remove()
- List: Removes the element with the specified value. If the target element does not exist, a ValueError will be reported
- Collection: Removes the element with the specified value. If the target element does not exist, a KeyError will be reported
- The return value is None
discard()
- Collection: Removes the element with the specified value. The target element does not exist, and no error will be reported
- The return value is None
clear()
- Remove all elements, making an empty list/set/dict
- The return value is None
of the
- Delete the entire tuple/list/set/dictionary
- No memory address allocation
4. Change
- Lists and tuples support combination using + and *
- Sets support intersection, union and difference for combination
- Lists and dictionaries support directly changing element values using indexes or keys
5. Check
- Tuples, lists, and dictionaries support finding element values using indexes or keys
- The dictionary supports the get() method to obtain the value corresponding to the key. When the key exists, the corresponding value is returned; when the key does not exist, None is returned, and KeyError will not be raised.
- Tuples, lists, sets, and dictionaries all support in and not in to determine whether an element exists.
- Tuples, lists, sets and dictionaries all support the use of loops to traverse elements
- Tuples and lists support using the index() method to view the index value of the target element. If the target element does not exist, a ValueError error will be reported
- Tuples and lists support the count() method to count the number of target elements
6. Sorting
- The list supports internal elements to be sorted using the sort() method. By default, numbers are sorted by size, and strings are sorted alphabetically. You can also specify a sorting method. Support key and reverse parameters
- The list supports using the reverse() method to reverse the internal elements, only for the index value, and the elements are not compared and sorted