# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/container Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/container/en.mdx Output docs content for large language models. --- export const metadata = { title: "Containers", description: "Learn Python container data types like list, tuple, dictionary, and set. Complete guide to indexing, slicing, and data manipulation with practical examples.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/18/2025", subject: "AI Programming", }; ## Container Concepts in Python Containers are data types that can store collections of items or elements. Imagine containers like storage boxes that can hold various kinds of items. Python provides several types of containers with different ways of organizing and manipulating data. Containers in Python have the main characteristic of being able to store various types of objects in one container. Each type of container has its own advantages depending on program needs. Python provides five main container types with different characteristics: - **String** - sequence of characters that cannot be changed - **List** - sequence of items that can be changed and indexed - **Tuple** - sequence of items that cannot be changed - **Dictionary** - key-value pairs - **Set** - collection of unique items without order ## Introduction to Lists List is the most flexible container in Python. List has three important characteristics: ordered (sequential), heterogeneous (can hold various data types), and mutable (can be changed). Lists in Python are similar to arrays in C or vectors in C++, but with more flexible capabilities because they can hold objects of different data types in the same list. ### List Characteristics | Characteristic | Explanation | Example | |---------------|-------------|---------| | Ordered | Elements have fixed order | `[1, 2, 3]` different from `[3, 2, 1]` | | Heterogeneous | Can hold various data types | `[1, 'text', 3.14, True]` | | Mutable | Contents can be changed after creation | Can add, remove, or modify elements | ### List Creation Syntax Lists are created using square brackets with elements separated by commas. Lists can contain items of different data types or even other lists. ## List Indexing Indexing is a way to access individual elements in a list using position numbers. Python uses zero-based indexing, meaning the first element has index 0. Indexing in Python has special rules that need to be understood: - **Positive indices** start from 0 for the first element - **Negative indices** start from -1 for the last element - **Indices out of range** will result in IndexError Lists and strings have similarities in terms of element access. Both support indexing and slicing with the same patterns. | Operation | String | List | |---------|--------|------| | **Indexing** | Get individual characters | Get individual elements | | **Slicing with stride** | Get substring | Get sub-list | The main difference is that strings are immutable (cannot be changed) while lists are mutable (can be changed). Because of this mutable nature, lists have various methods for data manipulation. B[Adding] A --> C[Removing] A --> D[Organizing] B --> E[append
extend
insert] C --> F[pop
remove] D --> G[sort
reverse
copy] `} /> | Method | Description | Usage Example | |--------|-------------|---------------| | `append(element)` | Add element at end of list | Adding new item | | `extend(list2)` | Merge with elements from another list | Combining two lists | | `insert(index, element)` | Insert element at specific position | Adding in middle of list | | `index(element)` | Find first index of element | Finding item position | | `pop()` | Remove and return last element | Taking last item | | `reverse()` | Reverse list order directly | Reversing order | | `remove(element)` | Remove first occurrence of element | Removing specific item | | `sort()` | Sort list directly | Sorting data | | `copy()` | Create list copy | Duplicating list | | `count(element)` | Count occurrences of element | Counting frequency | ## Special List Types Python supports several special list types with different characteristics and uses: 1. **Empty Lists** Empty lists are useful as initial containers to be filled with data later. There are two ways to create empty lists with the same result. 2. **Heterogeneous Lists** Heterogeneous lists can hold elements of different data types in one container. This provides high flexibility in data storage. 3. **Nested Lists** Nested lists are lists that contain other lists as elements. This concept enables creation of multidimensional data structures like matrices or tables. Nested lists are very useful for representing structured data like tables, matrices, or other hierarchical data. Accessing elements in nested lists uses multiple indexing like `nested[1][2]` to access elements at deeper levels. ## Other Containers Besides lists, Python provides three other container types with different characteristics and uses. ### Tuple Tuple is a data structure similar to list but immutable. Tuple supports packing and unpacking operations useful for data exchange. Although tuples are immutable, tuples can contain references to mutable objects like lists. An important concept in tuples is the difference between the immutable tuple itself and the mutable objects it contains. Tuples cannot change their structure, but the contents of mutable objects within them can still be modified. | Aspect | Tuple | List | |-------|-------|------| | **Mutability** | Immutable | Mutable | | **Syntax** | `()` or without parentheses | `[]` | | **Usage** | Fixed data, coordinates | Changing data | ### Dictionary Dictionary stores data in key-value pairs. Each key must be unique and is used to access the associated value. Dictionary uses curly braces `{}`. Dictionary maintains insertion order since Python 3.7+