Python Data Structures: Lists, Tuples, Sets, Dictionaries

In this eighth article of our beginner-friendly Python tutorial series, we'll explore Python data structures: lists, tuples, sets, and dictionaries. Data structures are essential for organizing and manipulating data in your programs. By understanding Python data structures, you can write more efficient and effective code.

Lists

Lists are ordered, mutable collections of elements, which can be of different types. You can create a list using square brackets []. Here's an example:

my_list = [1, "apple", 3.14]
print(my_list)  # Outputs [1, "apple", 3.14]

Access elements using indices, and modify them using assignment:

print(my_list[1])  # Outputs "apple"
my_list[1] = "banana"
print(my_list)     # Outputs [1, "banana", 3.14]

Tuples

Tuples are ordered, immutable collections of elements, similar to lists but cannot be modified after creation. Create a tuple using parentheses ().

my_tuple = (1, "apple", 3.14)
print(my_tuple)  # Outputs (1, "apple", 3.14)

Access elements using indices, but remember, tuples cannot be modified:

print(my_tuple[1])  # Outputs "apple"

Sets

Sets are unordered, mutable collections of unique elements. Create a set using curly braces {} or the `set()` constructor. Sets do not support indexing.

my_set = {1, 2, 3, 2, 1}
print(my_set)  # Outputs {1, 2, 3} (duplicates removed)

Add and remove elements using the `add()` and `remove()` methods:

my_set.add(4)
print(my_set)  # Outputs {1, 2, 3, 4}
my_set.remove(1)
print(my_set)  # Outputs {2, 3, 4}

Dictionaries

Dictionaries are unordered, mutable collections of key-value pairs. Create a dictionary using curly braces {} with key-value pairs separated by colons.

my_dict = {"one": 1, "two": 2, "three": 3}
print(my_dict)  # Outputs {"one": 1, "two": 2, "three": 3}

Access values using their keys, and modify them using assignment:

print(my_dict["two"]) # Outputs 2
my_dict["two"] = 22
print(my_dict) # Outputs {"one": 1, "two": 22, "three": 3}

Add new key-value pairs by simply assigning a value to a new key:

my_dict["four"] = 4
print(my_dict) # Outputs {"one": 1, "two": 22, "three": 3, "four": 4}

Remove a key-value pair using the `del` keyword:

del my_dict["two"]
print(my_dict) # Outputs {"one": 1, "three": 3, "four": 4}

 

Now that you've learned about Python data structures, you can better organize and manipulate data in your programs. Understanding lists, tuples, sets, and dictionaries is essential for writing efficient and effective Python code. In the next article, we'll explore Python classes and objects, which will help you write more organized, reusable, and modular code. Stay tuned!

Table of Contents: Python for Beginners

  1. Getting Started with Python: Installation and Basics
  2. Python Data Types and Variables: An Introduction
  3. Python Conditionals and Loops: Control Your Code
  4. Python Functions: Organize and Reuse Your Code
  5. Python Modules and Packages: Manage and Organize Your Projects
  6. Python File Handling: Read and Write Files
  7. Python Error Handling: Using Try and Except
  8. Python Data Structures: Lists, Tuples, Sets, Dictionaries
  9. Python Object-Oriented Programming: Classes and Objects
  10. Final Project: Build a More Complex Python App from Scratch