Join the conversation
Data structure Order mutability duplicate items access
list yess yess yess index
tuple yess no yess index
set no yess no no
dictonaries yess yess values can be duplicate but keys must be unique key
Reply
Sir, I would like to thank you, and appreciate your efforts in explaining concepts and teaching us python, Machine Learning, Data Science and many more other topics in a comprehensive manner like HALWA.
Reply
In Python, data structures are used to organize and store data efficiently. The primary data structures include lists, tuples, sets, and dictionaries. Here’s a brief comparison highlighting their similarities and differences:
1. Lists:
• Ordered: Elements have a defined order.
• Mutable: Elements can be changed.
• Indexable: Access elements using an index.
• Duplicates: Allows duplicate elements.
• Example: [1, 2, 3, 2]
2. Tuples:
• Ordered: Elements have a defined order.
• Immutable: Once created, elements cannot be changed.
• Indexable: Access elements using an index.
• Duplicates: Allows duplicate elements.
• Example: (1, 2, 3, 2)
3. Sets:
• Unordered: No defined order for elements.
• Mutable: Elements can be added or removed.
• Non-indexable: Cannot access elements using an index.
• Unique: Does not allow duplicate elements.
• Example: {1, 2, 3}
4. Dictionaries:
• Unordered: Insertion order is preserved in Python 3.7+.
• Mutable: Keys and values can be changed.
• Key-Value Pairs: Access elements using keys.
• Unique Keys: Keys must be unique; values can be duplicated.
• Example: {'a': 1, 'b': 2}
Similarities:
• Lists and Tuples: Both are ordered and can contain duplicates.
• Sets and Dictionaries: Both use hash tables for fast lookups.
• Mutability: Lists, sets, and dictionaries are mutable, allowing for dynamic changes.
Differences:
• Order: Lists and tuples are ordered, while sets and dictionaries are unordered (with dictionaries preserving insertion order from Python 3.7+).
• Mutability: Lists and dictionaries are mutable, tuples are immutable, and sets are mutable but do not allow duplicates.
• Access: Lists and tuples are accessed by index, sets by membership testing, and dictionaries by keys.
Reply
(1)List: "data structure that stores the elements in columns of a single row or multiple rows."Symbol = [], Mutable(entries can be change/duplicate), created by "list()" function, Access by Index, ordered, Example = [1,2,3](2) Set: "data structure which stores the elements in a single row."
Symbol = {}, Mutable(entries can be change),duplication not possible, created by "set()" function, Access by Index, ordered , Example = {1,2,3}(3) Tuple: "data structure that stores elements in columns of a single row or multiple rows."
Symbol = (), immutable(entries can't be change/duplicate), created by "set()" function, Access by Index, not ordered, Example = (1,2,3)(4) Dictionary: "Data structure that stores key-value pairs."
Symbol = {}, Mutable(entries can be change/update) but duplicate keys not allowed, duplicate values are possible, created by "dict()" function, Access by Keys, ordered(python version >3.7) , Example = {a : 1,b :2, c:3}
Reply
List of string
Reply
List
Reply
List is an ordered collection of items and mutable, enclosed in square brackets and each individual item can be accessed by its index.Tuple is an ordered collection of items and immutable, enclosed in parenthesis and each individual item can be accessed by its index.Set is an unordered collection of items and mutable, enclosed in curly-brackets and each individual item cannot be accessed.Dictionary is an ordered collection of items and mutable, enclosed in curly-brackets and each individual item can be accessed by its key.
Reply
Set: items are stored in curly braces, you can not change the items, there is no order, are immutablelist: vice versa
Reply
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Data Structure | Ordered | Mutable | Elements | Unique | Example |
| | Collection | | of | Elements | |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Lists | Yes | Yes | Mixed | No | [1, 2, 3] |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Tuples | Yes | No | Mixed | No | (1, 2, 3) |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Sets | No | Yes | Mixed | Yes | {1, 2, 3} |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Dictionaries | No | Yes | Key-Value | Keys (Yes) | {'a': 1} |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Strings | Yes | No | Characters| No | 'Hello' |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Arrays | Yes | Yes | Homogeneous| No | array('i') |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Queues | Yes | Yes | Mixed | No | Queue() |
+----------------------+-------------+-------------+-----------+--------------+-------------+
| Stacks | Yes | Yes | Mixed | No | deque() |
+----------------------+-------------+-------------+-----------+--------------+-------------+
Reply
| Data Structure | Similarities | Comparisons |
|---|---|---|
| **Lists** | * Ordered collection of items * Can contain any type of data * Mutable | * Similar to arrays in other languages * Can be used for a variety of purposes, such as storing data or creating queues |
| **Tuples** | * Ordered collection of items * Can contain any type of data * Immutable | * Similar to lists, but cannot be changed once created * Often used to store data that should not be modified |
| **Sets** | * Unordered collection of unique items * Can contain any type of data * Mutable | * Similar to lists, but do not allow duplicate items * Often used to store unique values, such as the set of all unique words in a document |
| **Dictionaries** | * Unordered collection of key-value pairs * Keys must be unique * Values can be any type of data * Mutable | * Similar to lists, but use keys to access values * Often used to store data that is associated with a key, such as a dictionary of words and their definitions |
| **Stacks** | * Last-in, first-out (LIFO) collection of items * Can contain any type of data * Mutable | * Similar to lists, but items can only be added or removed from the end of the stack * Often used to implement recursion or to store temporary data |
| **Queues** | * First-in, first-out (FIFO) collection of items * Can contain any type of data * Mutable | * Similar to lists, but items can only be added to the end of the queue and removed from the front of the queue * Often used to implement queues or to store data that needs to be processed in order |
Reply