Difference Between List Tuple, And Dictionary In Python
Lists:
Mutable: Lists are mutable, that is, they can be changed once created.
Syntax: They can be created using square brackets [].
Example :
my_list = [1, 2, 3, 'a', 'b', 'c']
Main Features:
Sequential: Items in a sequence are arranged in order.
Active: The length of a sequence can change by adding or deleting items.
Ways: Different ways are available for modifying sequences through built-in functions like append(), extend(), pop() and remove().
Tuples:
Not changing: They are unchangeable, because when a tuple is made, the elements cannot be altered.
Syntax: Made by using () brackets.
Example:
my_tuple = (1, 2, 3, 'a', 'b', 'c')
Main Features:
Organized: Tuples retain order as lists do.
Unchangeable: When a tuple is created, no elements can be altered.
Applications: tuples are commonly used for static groups of values like coordinates or settings.
Dictionaries:
Mutable: Mutable means that dictionaries allow adding, deleting or modifying key-value pairs.
Syntax: Curly braces {} are used to create a dictionary.
Example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Main Features:
Unordered: There is no specific order of elements guaranteed by dictionaries.
Key-Value Pairs: Each value in the dictionary is associated with a unique key.
Use Cases: The best option when it comes to representing mappings or associations between data.
Use Cases:
Lists: If you want an ordered collection of items that can be changed.
Tuples: In case you need an ordered collection of items that should not change.
Dictionaries: Use dictionaries for key-value mappings and associations.