The types of collections in Python
Overview
Python programming language has four built-in types of collections, namely List, Set, Tuple, and Dictionary.
List
- A
listis an ordered sequence of elements that store multiple values inside a single variable. - Square brackets
[]are used to declare it. - It is changeable, mutable, and allows duplicate values.
Syntax
listName = [value1, value2, ...]
Example
# list declarationstudents = ['Shubham', 'Parth', 'Pratik', 'Shubham']print("List")print(students)# adding value to liststudents.append('Rohan')print("\nAfter adding value to list")print(students)
Explanation
- Line 2: We declare a
listcalledstudents, and initialize it with a few values. - Line 4: We print the
liston the console - Line 7: We use the
append()method to add a new value to thelist. - Line 9: After adding the value, we print the
liston the console again.
The output shows that students contains the value Shubham twice. Thus, it allows duplicates values.
After creating the list, we can add values. Therefore, it is mutable.
Set
- A
setstores multiple values inside a single variable. - Curly brackets
{}are used to declare it. - It is unordered, mutable, and doesn’t allow duplicate values.
Syntax
setName = {value1, value2, ...}
Example
# set declarationstudents = {'Shubham', 'Parth', 'Pratik', 'Shubham'}print("Set")print(students)# adding value to setstudents.add('Rohan')print("\nAfter adding value to Set")print(students)
Explanation
- Line 2: We declare a
setcalledstudents, and initialize it with a few values. - Line 4: We print the
seton the console. - Line 7: We use the
add()method to add a new value to theset. - Line 9: After adding the value, we print the
seton the console again.
The output shows that students contains the value Shubham only once, even though we added it twice. This shows that it doesn’t allow duplicate values.
After creating the set, we can add values. Therefore, it is mutable.The set does not maintain insertion order; it is unordered.
Tuple
- A
tuplestores multiple values inside a single variable. - We use round brackets
()to declare it. - It is immutable, ordered, and allows duplicate values.
Syntax
tupleName = (value1, value2, ...)
Example
# tuple declarationstudents = ('Shubham', 'Parth', 'Pratik', 'Shubham')print("Tuple")print(students)
Explanation
- Line 2: We declare a
tuplecalledstudents, and initialize it with a few values. - Line 4: We print the
tupleon the console
The output shows that students contains the value Shubham twice. Therefore, it allows duplicate values.
After creating the tuple, we cannot add or remove any values. Thus, it is immutable.
Dictionary
- A
dictionarystores data in the form of key-value pairs. - Curly brackets
{}are used to declare it. - It is ordered, mutable, and doesn’t allow duplicate values.
Syntax
dictionaryName = {
key1: value1,
key2: value2,
...
}
Example
# dictionary declarationstudents = {'student1' : 'Shubham','student2' : 'Parth','student3' : 'Pratik','student1' : 'Shubham',}print('Dictionary')print(students)# add key-value pair to dictionarystudents['student5'] = 'Rohan'print('\nDictionary after adding new key-value pair')print(students)
Explanation
- Line 2: We declare a
dictionarycalledstudentsand initialize it with a few key-value pairs. - Line 8: We print the
dictionaryon the console. - Line 11: We add a new key-value pair to the
dictionary. - Line 13: After adding the key-value pair, we print the
dictionaryon the console again.
In the output, students contains the key-value pair ‘Shubham’ only once, even though we added it twice. Therefore, it doesn’t allow duplicate values.
After creating the dictionary, we can add more values. Thus, it is mutable.