WP Ad Inserter plugin for WordPress

Collections in Python

Read Time:16 Minute, 55 Second

Table of Contents

The collection module in Python has different containers. A container is an object that holds a number of other objects and provides a way to access the objects contained in them. The container then iterates over the items. Python examples of the built-in containers are ‘Tuples,’ ‘Lists,’ ‘sets,’ and ‘Dictionaries .’This article has been tailored to help you understand the built-in containers in the python collection.


It is an unordered, changeable collection that does not allow duplicates. It stores the mappings of unique keys to values. This container is written with curly braces,{}, and commas separate the key-value pairs. A dictionary has several sub-classes. The sub-classes are discussed below.


This subclass is used to keep elements in an iterable form- such that the keys represent elements in the iterable. In contrast, the values represent the count of elements in the iterable. A counter is equivalent to a multi-set of other languages.
Syntax of a counter:class collections . Counter([iterable-or-mapping])

from collections import Counter
dict_count = Counter({'a':3, 'b':2, 'c':1})

How to initialize counter objects

The counter object is initialized using the counter() function, which can be called in any of the ways below:

  • With a dictionary that contains keys and counts.
  • With keyword arguments that map the string names to counts.
  • With sequences of items.

A counter can also be used to count the distinct elements of a list or any other collections like a list as follows:


from collections import Counter
list_count = Counter(['a','b','c','a','b','a'])

or a tuple

tuple_count = Counter(('a','b','c','a','b','a'))

or a string

string_count = Counter("code underscored")

Using keyword parameters, we may manually feed the counts as in the following example:

keyword_count = Counter(x = 1, y = 2, z = 3)

We can also declare a Python Counter that is empty. We use the update() method to add values to it later.

def_count = Counter() # here, we define an empty Counter before adding values
def_count.update("z") # adding values to def_count

The update() method merely updates an existing counter with new numbers. We can keep updating this counter and adding more values. Consider the following scenario:




This sub-class remembers the order in which the keys are inserted. Syntax of an OrderedDict: class collections. OrderDict()
In this sub-class, while deleting and re-inserting items, the same key pushes the key to the end to maintain the order of the key inserted.

# demonstrating how the OrderedDict works from collections import OrderedDict print("Normal Dictionary:n")
dict = {}
dict['x'] = 2
dict['y'] = 4
dict['w'] = 1
dict['z'] = 3 for key, value in dict.items(): print(key, value) print("n Ordered Dict Demo:n") ord_dict = OrderedDict()
ord_dict['x'] = 2
ord_dict['y'] = 4
ord_dict['w'] = 1
ord_dict['z'] = 3 for key, value in ord_dict.items(): print(key, value)


The DefaultDict sub-class provides some default values for the key, which is non-existent, and it never raises a KeyError. Syntax for DefaultDict: class collections . defaultdict(default_factory)
A default_factory is a function that provides the default value for the dictionary created. In case this parameter is absent, then a KeyError is raised. DefaultDict objects are initialized using DefaultDict() method by passing the data type as an argument.

# program demonstrating the defaultdict from collections import defaultdict # this method returns default values for keys not present
def defValue(): return "Not Present" # dictionary definition
dict = defaultdict(defValue)
dict["x"] = 3
dict["y"] = 4 print(dict["x"])


This sub-class encapsulates many dictionaries to a single unit and returns a list of dictionaries. Syntax for ChainMap: class collections . ChainMap(dict1, dict2)
The values from a ChainMap are accessed by using the key name or by using the keys() and values() method.

# program demonstrating the ChainMap from collections import ChainMap dict_one = {'a': 1, 'b': 2}
dict_two = {'c': 3, 'd': 4}
dict_three = {'e': 5, 'f': 6} # chainmap definition
_chain = ChainMap(dict_one, dict_two, dict_three) print(_chain)


A UserDict acts as a wrapper around the objects of a dictionary. This container is used when one wants to create a dictionary that has modified functionality. Syntax for UserDict: class collections . UserDict([initialdata])

# program demonstrating the userdict from collections import UserDict dict = {'x':1,'y': 2,'z': 3} # UserDict creation
user_dict = UserDict(dict)
print(user_dict.data) # Creation of an empty UserDict
user_dict = UserDict()
print(user_dict .data)

How to add a new dictionary

A new dictionary is added by using the new_child() method. The newly added dictionary is added at the start of the ChainMap.


rontend_lang = {'JavaScript' : 'JS', 'Cascading Style Sheets' : 'CSS'}
new_lang = {'Hypertext Markup Language' : 'HTML'}
chain = col.ChainMap(backend_lang, frontend_lang)
print("Initial Chain: " + str(chain.maps))
chain = chain.new_child(new_lang)

How to determine the dictionary length

To determine the number of items that a dictionary has, one has to use the len() function, for example, print(len(dict)).

dict ={'a':3, 'b':2, 'c':1}

How to add elements to a dictionary

The addition of elements in a Python dictionary is done in various ways. To add one value at a time, define a value along with the key, i.e., Dict[Key] = ‘value .’The update() method is used to update an existing value in a dictionary. A new Key with the value is added to the dictionary unless the key-value exists. If it does exist, the value is updated. Nested keys are also added to an existing dictionary.

student_scores = { "Anthony": 89, "Ann": 64, "James": 34, "Mary": 83
} student_scores["Mathew"] = 75 print(student_scores)

How to access elements from a Dictionary

To access items of a dictionary, refer to the key name. The Key is used inside the square brackets. By using indexing [] syntax, you will be able to access the value of any key in the nested dictionary. get() method also helps to access elements from a dictionary.

# using indexing
car_dict =	{ "brand": "Benz", "model": "Sedan", "year": 2020
car_model = car_dict["model"]
print("car model is:", car_model) # using get
car_dict = { "brand": "Benz", "model": "Sedan", "year": 2020
car_model = car_dict.get("model")

Removing elements from Dictionary

Deletion of keys in Python can be done using the del keyword. By using it, specific values from a dictionary and the whole dictionary are deleted. The del keyword also deletes items in a Nested dictionary. To clear all items from a dictionary at once, use the clear() method. Pop() method returns and then deletes the value of the key that has been specified, while the popitem() method is used to remove and return an arbitrary (key, value) pair from the dictionary.

car_dict = { "brand": "Benz", "model": "Sedan", "year": 2020
car_dict .pop("model")
print(car_dict )

The popitem() method removes the last inserted item. However, a random item is deleted in versions before 3.7. Below is a demo.


car_dict =	{ "brand": "Benz", "model": "Sedan", "year": 2020

Using del keyword to delete an item in car_dict:

car_dict =	{ "brand": "Benz", "model": "Sedan", "year": 2020
del car_dict ["model"]
print(car_dict )

The del keyword is also used to remove the dictionary as follows:

car_dict = { "brand": "Benz", "model": "Sedan", "year": 2020
del car_dict

The clear() function clears the dictionary:

car_dict = { "brand": "Benz", "model": "Sedan", "year": 2020

Other Dictionary Operations

The Dictionary Membership Test is only for the keys and not values. To test if a key is in a dictionary or not, use the keyword in. Then use a for loop to iterate through each key in a dictionary.

car_dict = { "brand": "Benz", "model": "Sedan", "year": 2020
if 'model' in car_dict: print("Hurrah!!")
else: print("Item does not exit")

Dictionary Built-in-functions

They are commonly used with dictionaries to perform different tasks. Examples of the built-in-functions are:


  • all(): returns True if all keys in the dictionary are True or if the dictionary is empty.
  • len(): this method returns the length of items in the dictionary.
  • sorted(): it returns a new sorted list of keys in the dictionary.
  • any(): this method returns true if any dictionary key is true. The method returns false if the dictionary is empty.


These are data structures that store an ordered sequence of values. They are unchangeable and are defined with parentheses(). Syntax for tuples: class collections . namedtuple(typename, field_names)

Conversion operations for tuples.

  • _make() : this function returns a named tuple() from the iterable passed as argument.
  • _asdict() : this function returns the OrderDict constructed from the values mapped in a named tuple().

How to access Tuple elements : indexing

The index used must be an integer. In tuple, the index starts from 0; therefore, a tuple with six elements will have indices from 0 to 5. If you try to access an index outside the tuple, you will get an IndexError. Other types such as float will result in TypeError. Likewise, the nested tuples are accessed using nested indexing.

programming_lang = ('Python', 'Java','C++','JavaScript','Kotlin' );

Negative indexing

The last item is always index -1, and the second last item is -2, and so on.

programming_lang = ('Python', 'Java','C++','JavaScript','Kotlin' );


A range of items is accessed using the slicing operator colon.


programming_lang = ('Python', 'Java','C++','JavaScript','Kotlin' );

Changing a tuple

Tuples are immutable, meaning that you cannot change their elements once they have been assigned. However, it is possible to assign a tuple to different values. It is called reassignment. Further, to combine two tuples, use the + operator. The latter is referred to as concatenation. Using the * operator, you can repeat the elements in a tuple for a given number of times.

comp_companies = ("Facebook", "Apple", "Amazon")
updated_companies = list(comp_companies)
updated_companies[1] = "HP"
new_companies_tup = tuple(updated_companies) print(new_companies_tup)

Deleting a tuple

Since you cannot change items in a tuple, the items can neither be deleted nor removed. However, it is possible to delete a tuple entirely by using the keyword del.

comp_companies = ("Facebook", "Apple", "Amazon")
del comp_companies

Tuple membership test

We use the keyword ‘in’ to test if an item exists or not. When we use the keyword, we can iterate through each item in a tuple.

# testing if an item exists in a tuple
code_tuple = ('c', 'o', 'd', 'e', 's',) # Item is in operation
print('c' in code_tuple )
print('s' in code_tuple ) # Item is not in operation
print('a' not in code_tuple )


The collection list is created by placing elements in square brackets,[], and separated by commas. An index contains any number of items of different types, such as an integer, float, or string. A list can also be nested, meaning another list as an item.

How to access elements in a list


Different ways are used to access the elements of a list. Some of the methods are discussed below:

List index

The index operator,[], is used to access items in a list. In Python, indices begin from 0, meaning that a list with five elements will have an index from 0 to 4. If you try to access indexes other than these, you will get an index error. The indexes should be integers. Once you introduce afloat or different types, you will get a typeerror. If you want to access nested lists, we use nested indexing.

laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo'] # look for the index of 'DELL'
index = laptop_brands.index('DELL')

Negative indexing

Python allows negative indexing for its succession. The last item is -1, and the second last item is -2, and so on.

laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo']


The slicing operator: is used to access a range of items in a list. When you slice a list, the start index is inclusive, whereas the end index is exclusive. For example, my_list[2: 5] will return a list with elements at indexes 2, 3, and 4 but not 5.

laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']

How to add or change elements in a list

Lists are mutable. It means that their elements can change, unlike tuples. We use the operator, =, to change a range of items in a list. Similarly, we use the append() method or the extend() method to add several items to the list. The + operator is used to combine two lists. It is known as concatenation. To repeat a list for any given no of times, we use the * operator. Using the insert() method, we can insert one item at a desired location or insert multiple elements by squeezing it into an empty list.


laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']

How to delete items in a list

Python del statement is used to delete one or more items from a list. In contrast, the remove() operator will remove a given item, but if you want to remove an item at a given index, use the pop() operator. Given that the last index is not provided, the pop() method will remove and return the last item. If we have to empty a whole list, the clear() method is used.

#using remove()
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
# using pop() and item index
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
print(laptop_brands )
# using pop()
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
# using del keyword to delete an item given the index
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
del laptop_brands [0]
print(laptop_brands )
# using del keyword
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
del laptop_brands print(laptop_brands)
# using clear()
laptop_brands = ['Apple', 'HP', 'DELL', 'Lenovo', 'IBM','Chromebook']
print(laptop_brands )


It is a container that acts as a wrapper around the objects in a list. It is useful when one wants to create a list that has modified functionality. Syntax: class collections . UserList([list])

# program demonstrating the userlist
from collections import UserList num_list = [11, 12, 13, 14] # userlist creation
user_list = UserList(num_list)
print(user_list.data) # Creating empty userlist
user_list = UserList()

Other python list methods

The reverse() method reverses the order of items in a list, while the sort() method arranges things in ascending order. To return a shallow copy of the list, we use the copy() method. In addition, a for loop is used to iterate through each item in a list. By using the keyword in, we can test if an object exists in a list or not.


A set is an unordered collection of not changeable items and has no duplicates. Set items are immutable, but you can add or remove items from the set. Sets perform mathematical set operations such as intersection, symmetric difference, and union. They are created when items are placed inside curly braces or using the built-in function, set() and separated by commas.

A set can have any number of things, and they may be of different types, for example, integer, float, string, and tuple. It is challenging to create an empty set since curly braces will result in an empty dictionary. Therefore, we use the set() function without any argument to make a set with no elements.


Modifying sets

Set data types do not support accessing, changing, or accessing set elements using slicing or indexing. Indexing in sets has no meaning since sets are unordered. To add a single element to a set, use the add() method, but if you want to add multiple elements, use the update() method.

# using add()
operating_system_set = {"Linux", "MacOS", "Windows"}
# using update()
operating_system_set = {"Linux", "MacOS", "Windows"}
linux_distros_set = {"Ubuntu", "Fedora", "Kali"} operating_system_set.update(linux_distros_set )

Removing elements from a set

An item is removed from a set using discard() and remove() methods. The difference between the two is that in the discard() method, a set is left unchanged if the element is not in the set, whereas the remove() function raises an error if the element is not present. In addition, items can be removed and returned using the pop() method. There is no way of determining the item that will pop up since a set is an unordered data type meaning it is entirely arbitrary. The clear() method removes all items from a set.

operating_system_set = {"Linux", "MacOS", "Windows"} operating_system_set .remove("Linux") print(operating_system_set )

Set operations

The | operator is used to perform unions. Some operations can also be accomplished using the union() method. Given elements A and B, the union of A and B is a set of all the elements from both sets. In contrast, the intersection of A and B is a set of common elements in Wboth sets. In performing intersections, the & operator is used. The intersection() method can also accomplish the operations. The difference of set B from set A,(A-B) is a set of elements that are only in A and not B. The – operator performs differences of sets. The difference() method is also used. The symmetric difference of A and B refers to a set of elements in A and B. But not in both, meaning intersection is excluded. ^ operator is used to performing symmetric difference. Symmetric_difference also accomplishes the task.

Other python methods

Intersection() methods return the intersection of two sets as a new set, whereas the intersection_update() method will update the set with the intersection of itself and another. Issubset() method will return true if another set contains this set, whereas issuperset()will return true if this set contains another set. The isdisjoint() will return true if two sets have a null intersection.

Set membership test

By using the keyword in, we can test if an item exists in a set or not. We iterate through items using a for a loop.


Built-in functions of the set

Sets have built-in functions which perform different tasks. Examples of the built-in-functions are:

  • all(): it returns true if the set is empty or all set elements are true.
  • any(): it returns true if any element of the set is true, but if the set is empty, it will return false.
  • sum(): this operator will return the sum of all elements in the set.
  • sorted(): this method returns a new sorted list from elements in the set but does not sort the set itself.
  • min(): it returns the smallest item in the list.
  • max(): it returns the essential item in the list.
  • len(): this method is the length of items in the set.
  • enumerate(): this operator returns an enumerate object that contains index and value for all set items as a pair.


In Python, collections are container data types, such as lists, sets, tuples, and dictionaries. Depending on the declaration and usage, they have varied features.

A list is a type of data structure stated in square brackets, is changeable, stores duplicate entries, and can be accessed via indexes. Although duplicate items can exist within a tuple, it is ordered and immutable by nature.

A set is declared in square brackets and is not ordered. It is not indexed, and there are no duplicate entries. On the other hand, a dictionary is a changeable collection of key-value pairs. To declare a dictionary, we use square brackets.

Source: https://www.codeunderscored.com/collections-in-python/

Java Constructor Previous post Java Constructor
The Relevance of TypeScript in 2022 Next post The Relevance of TypeScript in 2022

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.