This document provides a concise overview of Python data structures, focusing on tuples, lists, dictionaries, and sets, and highlighting their properties operations, indexing, slicing, and manipulation techniques for data science applications.
This document summarizes the essential concepts of Python data structures, including tuples, lists, dictionaries, and sets. It covers their properties, operations, indexing, slicing, and manipulation techniques for effective data science applications.
Tuples are ordered, immutable collections defined with parentheses (). They can contain mixed data types and support both positive and negative indexing for element access. Operations such as concatenation and slicing are available, but any modification requires creating a new tuple. Tuples can be nested for complex data structures, and elements in nested tuples are accessed through multi-level indexing.
Lists are ordered, mutable collections represented with square brackets []. They can store elements of different types, including nested lists and tuples. Lists support positive and negative indexing, as well as operations like concatenation, appending, adding, deleting, and splitting elements. Modifying a list directly changes its contents. Lists can be cloned to create independent copies, and aliasing occurs when multiple names reference the same list object, causing changes in one to affect the other.
Dictionaries are flexible key-value pair collections, denoted by curly brackets {}. Keys must be unique and immutable, while values can be mutable or immutable and allow duplicates. Dictionaries support operations such as adding, deleting, and checking for the existence of keys, as well as retrieving lists of keys and values using methods.
Sets are unordered collections of unique elements, defined with curly brackets {}. They are useful for removing duplicates and performing set operations like union and intersection. The set() function converts a list to a set, ensuring all elements are unique. Set operations include adding, removing, and verifying elements, as well as combining sets using operators like & for intersection and union() for merging. The issubset() method determines if one set is a subset of another, supporting advanced data analysis and comparison tasks.
| Data Structure | Ordered | Mutable | Unique Elements | Syntax | Key Features |
|---|---|---|---|---|---|
| Tuple | Yes | No | No | ( ) | Indexing, slicing, nested tuples |
| List | Yes | Yes | No | [ ] | Indexing, slicing, list methods |
| Dictionary | No | Yes | Keys only | { } | Key-value pairs, dictionary methods |
| Set | No | Yes | Yes | { } | Set operations, uniqueness |