Collections in Python
Contents
Collections in Python#
Collections can be described as bags/boxes of values or containers while sequences are data structures, especially associated with storage and groupings that can be accessed easily, most of the sequences are a member of collections.
Types of Collections#
There are several different types of collections in Python. The collection data structure allows storing several objects with or without any certain order. You can add and remove stuff from a collection, and you can iterate over a collection. It’s a storage construct that allows you to collect things and perform operations on them.
Collections in Python are grouped into two types:
Sequence
Mapping
Python sequence#
Sequences are a major data type arrangement in Python, with a concept of order, indexed numerically from 0
, including string, list and tuple. Things always come out of them in the same order as they were put in. It’s worth noting that every sequence is a collection, but not every collection is a sequence.
Sequences have got two main features.
The first one is the slicing operation, obtaining a certain number of items from a list.
Indexing operation, allows a user to fetch a particular item or element in a list.
Indexing also gives a chance to perform operations on particular items.
Python mapping#
Mapping types collections, without a concept of order, indexed by keys, which include only one type, a dictionary. The ordering of items in sets is arbitrary (It’s technically not changeable). The ordering of keys and values in dictionaries is also random.
A collection is good because we can put more than one value in them and carry them all around in one convenient package. We have a bunch of values in a single ‘variable’.