Difference Between List, Tuple, Set and Dictionary in Python

1. Introduction

Python offers several data structures like lists, tuples, sets, and dictionaries, each with its unique characteristics. A list is an ordered and mutable collection of items. A tuple is similar to a list but immutable. A set is an unordered collection of unique items, and a dictionary is a collection of key-value pairs.

2. Key Points

1. Mutability: Lists and dictionaries are mutable; tuples and sets are immutable.

2. Ordering: Lists and tuples are ordered; sets and dictionaries are unordered.

3. Syntax: Lists use [], tuples (), sets {}, and dictionaries {key: value}.

4. Uniqueness: Sets enforce unique elements.

3. Differences

Feature List Tuple Set Dictionary
Mutability Mutable Immutable Immutable Mutable
Ordering Ordered Ordered Unordered Unordered
Syntax Square brackets [] Parentheses () Curly braces {} Curly braces {} with key: value
Uniqueness Allows duplicates Allows duplicates Unique elements only Keys are unique

4. Example

# Example of a List
my_list = [1, 'two', 3.0]

# Example of a Tuple
my_tuple = (1, 'two', 3.0)

# Example of a Set
my_set = {1, 2, 2, 3}

# Example of a Dictionary
my_dict = {'one': 1, 'two': 2, 'three': 3}

Output:

List:
[1, 'two', 3.0]
Tuple:
(1, 'two', 3.0)
Set:
{1, 2, 3}
Dictionary:
{'one': 1, 'two': 2, 'three': 3}

Explanation:

1. The list my_list shows a collection of ordered and mutable items.

2. The tuple my_tuple is similar to a list but immutable.

3. The set my_set shows unique elements only, even though 2 is added twice.

4. The dictionary my_dict stores data in key-value pairs with unique keys.

5. When to use?

- Use lists for an ordered collection of items that may need to change.

- Use tuples for ordered collections that do not need to change.

- Use sets when you need to ensure all elements are unique and order is not important.

- Use dictionaries for storing related information as key-value pairs for quick lookups.

Related Python Posts:

Difference Between Local and Global Variables in Python

Difference Between List and Tuple in Python

Difference Between Array and List in Python

Difference Between List and Dictionary in Python

Difference Between List, Tuple, Set and Dictionary in Python

Difference Between a Set and Dictionary in Python

Difference between for loop and while loop in Python

Difference Between pass and continue in Python

Difference Between List append and extend in Python

Difference Between == and is operator in Python

Difference Between Deep and Shallow Copy in Python

Class Method vs Static Method in Python

Class Method vs Instance Method in Python

Difference Between List and Set in Python

Difference Between Generator and Iterator in Python

Difference Between str and repr in Python

Method Overloading vs Overriding in Python

Difference Between Dictionary and Tuple in Python

Difference Between Dictionary and Object in Python

Difference Between Mutable and Immutable in Python

Difference Between Interface and Abstract Class in Python

Difference Between Python Script and Module

Difference Between for Loop and Iterator in Python

Comments