Python Collections – Tutorial with Examples

Collections in Python are a group of data structures that allow you to store and manage collections of data efficiently. The collections module in Python provides a variety of classes and functions that make it easier to work with collections of data. In this article, we will explore the various collections in Python, including lists, dictionaries, sets, and tuples, and learn how to use them in practical examples.

Lists

A list in Python is an ordered collection of items, which can be of any data type. Lists are declared using square brackets, with items separated by commas. Lists are mutable, meaning you can add, remove, or modify elements after a list has been created. Here is an example of a list in Python:

fruits = ["apple", "banana", "cherry"]
print(fruits)
# Output: ['apple', 'banana', 'cherry']

You can access elements of a list using indexing, starting from 0. You can also use negative indexing to access elements from the end of the list. Here is an example of indexing a list:

fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: 'apple'
print(fruits[-1]) # Output: 'cherry'

Lists also support various operations such as concatenation, repetition, and slicing. Here is an example of concatenating two lists:

fruits1 = ["apple", "banana", "cherry"]
fruits2 = ["date", "fig", "grape"]
fruits = fruits1 + fruits2
print(fruits)
# Output: ['apple', 'banana', 'cherry', 'date', 'fig', 'grape']

Dictionaries

A dictionary in Python is an unordered collection of key-value pairs, where keys must be unique. Dictionaries are declared using curly braces, with key-value pairs separated by colons. Here is an example of a dictionary in Python:

person = {"name": "John Doe", "age": 30, "country": "USA"}
print(person)
# Output: {'name': 'John Doe', 'age': 30, 'country': 'USA'}

You can access the values of a dictionary using the keys. Here is an example of accessing values of a dictionary:

person = {"name": "John Doe", "age": 30, "country": "USA"}
print(person["name"]) # Output: 'John Doe'
print(person.get("age")) # Output: 30

You can add, remove, or modify elements in a dictionary using the keys. Here is an example of modifying an element in a dictionary:

person = {"name": "John Doe", "age": 30, "country": "USA"}
person["age"] = 31
print(person)
# Output: {'name': 'John Doe', 'age': 31, 'country': 'USA'}

Sets

A set in Python is an unordered collection of unique items. Sets are declared using curly braces, with items separated by commas. Here is an example of a set in Python:

fruits = {"apple", "banana", "cherry"}
print(fruits)
# Output: {'banana', 'cherry', 'apple'}

Sets support operations such as union, intersection, and difference. Here is an example of finding the union of two sets:

fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"date", "fig", "grape"}
fruits = fruits1.union(fruits2)
print(fruits)
# Output: {'banana', 'cherry', 'date', 'fig', 'apple', 'grape'}

Tuples

A tuple in Python is an ordered collection of items, which can be of any data type. Tuples are declared using parentheses, with items separated by commas. Unlike lists, tuples are immutable, meaning you cannot add, remove, or modify elements after a tuple has been created. Here is an example of a tuple in Python:

person = ("John Doe", 30, "USA")
print(person)
# Output: ('John Doe', 30, 'USA')

You can access elements of a tuple using indexing, starting from 0. You can also use negative indexing to access elements from the end of the tuple. Here is an example of indexing a tuple:

person = ("John Doe", 30, "USA")
print(person[0]) # Output: 'John Doe'
print(person[-1]) # Output: 'USA'

Tuples can also be used to return multiple values from a function. Here is an example of returning multiple values from a function using a tuple:

def get_person_info():
    return ("John Doe", 30, "USA")
person = get_person_info()
print(person)

#Output: ('John Doe', 30, 'USA')

In conclusion, the collections in Python provide different options for managing and storing collections of data, each with its own strengths and limitations. Knowing when to use lists, dictionaries, sets, or tuples in your code can help make your code more efficient and effective.

Leave a Reply

Your email address will not be published. Required fields are marked *