What Are Collections in Python?
A collection is defined as an object or structure that contains a set of other objects. In programming, a collection is a container that contains multiple units. In Python, collections can be described as powerful instruments in programming that permit the user to store data in one variable, thus enabling the user to handle several data values at once. The role of collections in programming is crucial as it enables programmers to organize the data systematically for easy retrieval, modification, such as sorting, adding, deleting items in the collection, or changing the selected item.Among the most used types of collections in Python include collections of lists, tuples, and dictionaries. Every collection has its own characteristics, features, benefits, and disadvantages depending on the area which you are using it in.
Lists: A Collection That Is Dynamic and Changeable
A list is one of the most frequently used types of collection in Python. It is a sequence which is a representation of a collection of items which is changeable and ordered. Being a sequence, Lists are flexible because elements in them can be added, removed, or changed. Possible elements of a list are numbers, strings, and other lists.Lists are created by writing the items to be contained in it inside square brackets, `[]` separated by commas. Lists are indexed i.e., every item in a list is positioned (or indexed) such that the first item has an index of 0. You can access any item in a list by specifying the respective index of that item.
The list is carefully built which has one of the most fundamental attributes, which is, it's mutable. This means that elements of the list can be changed even though the list has already been built. You can put items in the list that were not there before, remove items that are in the list, or update items that are already in the list.
Here's an example of a list:
List Manipulation Example
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange') Adds 'orange' to the end of the list
fruits[1] = 'blueberry' Changes 'banana' to 'blueberry'
Also, lists support slicing, i.e., you can select a subsection of elements contained within the list by using a start and end index point. For example ':' means all columns from index one through index three excluding index three, meaning that the selection will be made from the first element up to the second. This returns a sub list with all elements from index one through , but not including index three.
Tuples — Non-Changeable and Sequenced Objects
Another collection type in Python is tuples, this is very similar to a list except that there's one major difference – it is tuples which are extremely economic in structure. Once a tuple is created no further edits can be made: elements can neither be changed nor be added nor anything be removed. When a program is run, it can therefore make use of a collection of data without worrying of the data changing at any time during the program.For creating a tuple you would simply require an opening bracket as opposed to a square bracket. They can still incorporate the same functionality as a list in that they are able to hold numerous items which can be of various types but when a tuple is created, that's it the contents cannot be changed.
A simple example of a tuple is shown below.
Tuple Example
colors = ('red', 'green', 'blue')
For example:
Tuple Immutability Example
colors[1] = 'yellow' This will cause an error since the tuples have been assigned and are thus immutable
Also due to the fact that constant elements are present in tuples, they are usually faster and use less memory than lists. These are often employed in Python when there is a need to safeguard the data and when speed is important.
Dictionaries: Storing value against a unique key!
A specific type of structured data storage with the name of dictionary stores elements in the form of key-value relationships. In contrast to lists and tuples where elements/indexes are leveraged to reach an element, retrieval of values from a custom key to rather a value is possible in a dictionary. This characteristic of dictionaries renders them a suitable candidate to use in any situation where data has to be pulled based on a unique identifier, quickly.A dictionary in python can be created by enclosing the key-value pairs in curly brackets, each pair being separated by a colon symbol. Key in regards to a particular value acts as a unique reference and each value represents data related to that specific key.
For instance consider a case of a simple python dictionary which describes attributes of a person:
Dictionary Example
person = {'name': 'John', 'age': 30, 'city': 'New York'}
print(person['name']) Output will be 'John'
Providing fast lookup is one of the primary advantages of dictionaries. Knowing its key allows you to know its value almost instantly, which makes dictionaries suitable for use in areas requiring fast searching – for example a database or a data mapping.
You can change dictionaries also by inserting new elements or modifying the old ones:
Dictionary Modification Example
person['age'] = 31 Sets the value of age to 31
person['job'] = 'Engineer' Sets the job for the person as a key-value pair
Dictionary Deletion Example
del person['city'] The key city and its value are removed