Data Structure Conversions
Learn to convert between tuples, lists, dictionaries, and sets using explicit methods in Python.
We'll cover the following...
Before diving into the in-built methods used to convert between data types, let's recap the differences between these data types. The following table summarizes the differences nicely.
Feature | Tuple | List | Dictionary | Set |
Bracket type |
|
|
|
|
Creation |
|
|
|
|
Mutability | Immutable | Mutable | Mutable | Mutable (elements are immutable) |
Order | Maintains order | Maintains order | No inherent order | No specific order elements are unique |
Duplicates | Allows duplicates | Allows duplicates | Keys must be unique; values may repeat | No duplicates |
Access | Indexing (my_tuple[0]) | Indexing (my_list[0]) | Key-based (my_dict['key1']) | No indexing; use iteration or in operator |
Operations | Limited concatenation, slicing | Extensive concatenation, slicing | Key-based operations | Set operations |
Use Cases | Fixed collection of items | A dynamic collection of items | Associative arrays, key-value pairs | Unique collection, membership testing |
Explicit conversion
The template for explicitly converting ...