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 list is 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 declaration
students = ['Shubham', 'Parth', 'Pratik', 'Shubham']
print("List")
print(students)
# adding value to list
students.append('Rohan')
print("\nAfter adding value to list")
print(students)

Explanation

  • Line 2: We declare a list called students, and initialize it with a few values.
  • Line 4: We print the list on the console
  • Line 7: We use the append() method to add a new value to the list.
  • Line 9: After adding the value, we print the list on 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 set stores 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 declaration
students = {'Shubham', 'Parth', 'Pratik', 'Shubham'}
print("Set")
print(students)
# adding value to set
students.add('Rohan')
print("\nAfter adding value to Set")
print(students)

Explanation

  • Line 2: We declare a set called students, and initialize it with a few values.
  • Line 4: We print the set on the console.
  • Line 7: We use the add() method to add a new value to the set.
  • Line 9: After adding the value, we print the set on 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 tuple stores 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 declaration
students = ('Shubham', 'Parth', 'Pratik', 'Shubham')
print("Tuple")
print(students)

Explanation

  • Line 2: We declare a tuple called students, and initialize it with a few values.
  • Line 4: We print the tuple on 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 dictionary stores 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 declaration
students = {
'student1' : 'Shubham',
'student2' : 'Parth',
'student3' : 'Pratik',
'student1' : 'Shubham',
}
print('Dictionary')
print(students)
# add key-value pair to dictionary
students['student5'] = 'Rohan'
print('\nDictionary after adding new key-value pair')
print(students)

Explanation

  • Line 2: We declare a dictionary called students and initialize it with a few key-value pairs.
  • Line 8: We print the dictionary on 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 dictionary on 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.