Collections in Python Are Basically Container Data Types, Namely Lists, Sets, Tuples, Dictionary
Collections in Python Are Basically Container Data Types, Namely Lists, Sets, Tuples, Dictionary
They have different characteristics based on the declaration and the usage. A list is declared in
square brackets, it is mutable, stores duplicate values and elements can be accessed using
indexes.
Nametuple-like object with named fields. These field attributes are accessible by lookup as well
as by index.
Signature:
collections.namedtuple(type_name, field-list)
The following statement declares a student class having name, age and marks as fields.
d1 = collections.OrderedDict()
d1['A'] = 65
d1['C'] = 67
d1['B'] = 66
d1['D'] = 68
deque()
A deque object support appends and pops from either ends of a list. It is more memory efficient
than a normal list object. In a normal list object, the removal of any item causes all items to the
right to be shifted towards left by one index. Hence, it is very slow.
A Counters is a container which keeps track to how many times equivalent values are added.
Python counter class is a part of collections module and is a subclass of dictionary.
Python Counter
We may think of counter as an unordered collection of items where items are stored as
dictionary keys and their count as dictionary value.
The “end” is a keyword that is used in python to place a space after the displayed string instead
of a newline. In python every time you print(“something”), by default it will append a newline.
print("world")
Output:-
Hello world
print(“World”)
Output:-
Hello
world