What are Collections in Python?

What are Collections in Python?

Blog | Board Infinity
Blog | Board Infinity

Introduction

Data structures like lists, tuples, arrays, dictionaries, etc. are examples of data structures that are usually referred to as collections in Python.

A built-in collections module in Python offers extra data structures for data collections.

The six most popular data structures are included in the collection modules.

Defaultdict

A dictionary in Python is just like Defaultdict. The only distinction is that when you attempt to access a key that doesn't exist, it doesn't throw an exception or a key error.

Even though the 4th index in the following code wasn't initialized, the compiler still gives us a value of 0 when we try to access it.

EXAMPLE :

from collections import defaultdict 
nums = defaultdict(int
nums['one'] = 1 
nums['two'] = 2
nums['three'] = 3
print(nums['four'])  

write your code here: Coding Playground

OUTPUT:

0

Counter

Counter A built-in data structure known as a counter is used to track the frequency of each value in an array or list.

The following code counts how many times the value 2 appears in the provided list.

EXAMPLE:

from collections import Counter 
list = [1,2,3,4,1,2,6,7,3,8,1,2,2
answer=Counter()
answer = Counter(list
print(answer[2])  

write your code here: Coding Playground

OUTPUT :

4

Deque

Deque is the best form of a list for adding and removing items. It can add/remove items from either start or the end of the list.

The supplied list is being added to at the conclusion of the following code, and g is at the beginning of the same list.

EXAMPLE:

from collections import deque 
#initialization
list = ["a","b","c"
deq = deque(list) 
print(deq) 

#insertion
deq.append("z"
deq.appendleft("g"
print(deq)
#removal
deq.pop() 
deq.popleft() 
print(deq)

write your code here: Coding Playground

OUTPUT:

deque(['a', 'b', 'c'])
deque(['g', 'a', 'b', 'c', 'z'])
deque(['a', 'b', 'c'])

Namedtuple ()

A very significant issue in the world of computer science is resolved by the Namedtuple() function. Unlike namedtuple(), which returns names for each place in the tuple, regular tuples don't need to remember the index of each field of a tuple object.

In the code below, passing an attribute is sufficient to produce the desired output and does not require an index to print a student's name.

EXAMPLE

from collections import namedtuple
Student = namedtuple('Student', 'fname, lname, age'
s1 = Student('Peter', 'James', '13'
print(s1.fname)

OUTPUT

Peter

ChainMap

ChainMap creates a list of dictionaries by combining numerous dictionaries. With no limitations on the number of dictionaries, ChainMaps essentially condenses several dictionaries into a single unit.

The next application, ChainMap, will give you back two dictionaries.

EXAMPLE

import collections

dictionary1 = { 'a' : 1, 'b' : 2
dictionary2 = { 'c' : 3, 'b' : 4
chain_Map = collections.ChainMap(dictionary1, dictionary2) 
print(chain_Map.maps)  

OUTPUT

[{'a': 1, 'b': 2}, {'b': 4, 'c': 3}]

OrderedDict

OrderedDict A dictionary that keeps its order is called OrderedDict. For instance, the order is maintained if the keys are put in a particular order. The location will not change even if you later modify the key's value.

EXAMPLE

from collections import OrderedDict 
order = OrderedDict() 
order['a'] = 1 
order['b'] = 2 
order['c'] = 3 
print(order) 

#unordered dictionary
unordered=dict()
unordered['a'] = 1 
unordered['b'] = 2 
unordered['c'] = 3
print("Default dictionary", unordered)

write your code here: Coding Playground

OUTPUT

OrderedDict([('a', 1), ('b', 2), ('c', 3)])
Default dictionary {'b': 2, 'a': 1, 'c': 3}

Python

Blog | Board Infinity

At Board Infinity we have authors leading in their profession sharing their insights, ideas and inspiration. Here influential thinkers, creators, makers and doers are found in one place.