We will continue to study the general principles of working with standard collections (it does not consider the collections module in it) Python. Will be considered ways to merge and update collections with the formation of a new or a change in the original, as well as ways to add and remove items in the modified collections.
For whom : for Python learners who already have an initial understanding of the collections and work with them, who want to systematize and deepen their knowledge, put them into a coherent picture.
Table of contents:
Combining strings, tuples, lists, dictionaries without changing the source.
1. Combining strings, tuples, lists, dictionaries without changing the source
Consider ways to combine strings, tuples, lists, dictionaries without changing the original collections - when a new collection of the same type is created from several collections without changing the original ones. ')
Combining strings and tuples (tuple) is possible using the addition operator "+"
For both types of sets (set, frozenset), different variants of the combination of sets are possible (the original sets do not change - a new set is returned).
# ( ) a = {'a', 'b'} b = { 'b', 'c'} # b
Union :
c = a.union(b) # c = b.union(a) # c = a + b # + # TypeError: unsupported operand type(s) for +: 'set' and 'set' c = a | b # print(c) # {'a', 'c', 'b'}
Intersection :
c = a.intersection(b) # c = b.intersection(a) c = a & b # print(c) # {'b'}
The intersection of more than 2 sets at once:
a = {'a', 'b'} b = { 'b', 'c'} c = { 'b', 'd'} d = a.intersection(b, c) # d = set.intersection(a, b, c) # ( ) print(d) # {'b'}
Difference (difference) - the result depends on how many of which we subtract:
c = a.difference(b) # c = a - b print(c) # {'a'} c = b.difference(a) # c = b - a print(c) # {'c'}
Symmetric difference (symmetric_difference) This is a kind of operation opposite to the intersection - selects elements from both sets that do not intersect, that is, all but the same:
c = b.symmetric_difference(a) # c = a.symmetric_difference(b) # c = b ^ a # print(c) # {'a', 'c'}
3. Combining a list, a dictionary and a variable set with a change in the original collection
For list
Add all the elements of the second list to the elements of the first with the change of the first list method. extend ():
a.extend(b) # a += b a.extend(b) print(a, b) # [1, 2, 3, 4, 5] [4, 5]
Add the second list as one item with the change of the first list method. append ():
a.append(b) # a += [b] a.append(b) print(a, b) # [1, 2, 3, [4, 5]] [4, 5]
To change the dictionary with the addition of elements of another dictionary method is used. update ().
Note: for matching dictionary keys, the values are updated:
For a variable set (set), in addition to the operations described in the previous section, their analogs are also possible, but with a change in the original set — these methods end with _update. The result depends on how many are updated.
. difference_update ()
a = {'a', 'b'} b = { 'b', 'c'} a.difference_update(b) print(a, b) # {'a'} {'b', 'c'} a = {'a', 'b'} b = { 'b', 'c'} b.difference_update(a) print(a, b) # {'a', 'b'} {'c'}
. intersection_update ()
a = {'a', 'b'} b = { 'b', 'c'} a.intersection_update(b) print(a, b) # {'b'} {'b', 'c'} a = {'a', 'b'} b = { 'b', 'c'} b.intersection_update(a) print(a, b) # {'b', 'a'} {'b'}
. symmetric_difference_update ()
a = {'a', 'b'} b = { 'b', 'c'} a.symmetric_difference_update(b) print(a, b) # {'c', 'a'} {'c', 'b'} a = {'a', 'b'} b = { 'b', 'c'} b.symmetric_difference_update(a) print(a, b) # {'a', 'b'} {'c', 'a'}
4 Adding and deleting items of mutable collections
Adding and deleting items to a collection is possible only for mutable collections: a list (list), a set (only set, not frozenset), a dictionary (dict). Moreover, for a list that is an indexed collection, it is also important at what position we add an item.
Notes:
Examples of using the .insert method (index, element)
If I somewhere made an inaccuracy or did not take into account something important - write in the comments, important comments will later be added to the article indicating your authorship.
If some points are not clear and clarification is required - write your questions in the comments - or I or other readers will give an answer, and efficient questions with answers will be later added to the article.