Difference Between List and Dictionary in Python
In Python, do you know how a list differs from a dictionary? While a list is like a row of boxes, a dictionary is more like a set of labeled drawers. Each has its unique use. Curious about which to use when? Let’s dive in and find out!
Table of Content
- Difference Between List and Dictionary in Python: List vs Dictionary
- What is List in Python?
- What is Dictionary in Python?
- Key Difference Between List and Dictionary in Python?
What is the Difference Between List and Dictionary in Python?
Parameter | List | Dictionary |
Definition | An ordered collection of items. | An unordered collection of data in a key: value pair form. |
Syntax | Uses square brackets []. | Uses curly braces {}. |
Ordering | Ordered: Items have a defined order, which will not change. | Unordered: Items do not have a defined order. |
Indexing | Accessed by index, starting from 0. | Values are accessed using keys. |
Mutability | Mutable: Items can be modified after creation. | Mutable: Values can be updated, and key: value pairs can be added or removed. |
Uniqueness | Allows duplicate items. | Does not allow duplicate keys. However, values can be duplicated. |
Data Types | It can store any data type. | Keys can be of any immutable data type (e.g., strings, numbers, tuples). Values can be of any type. |
Use Case | When order matters or when you need to store multiple values for an item. | When you need a unique key for each piece of data. |
Example | fruits = [“apple”, “banana”, “cherry”] | person = {“name”: “John”, “age”: 30} |
Built-in Functions | append(), remove(), pop(), sort(), etc. | keys(), values(), items(), get(), pop(), etc. |
Performance | Faster for ordered operations like sorting. | Faster for lookup operations due to the hash mapping of keys. |
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is List in Python?
Lists in Python are ordered collections of items and each item is assigned to an index value starting from 0. Lists are mutable, hence the elements can be modified after their creation.
To create a list in Python, you have to use square brackets []. Items in the lists can be of any data type, including numbers, strings, or even lists.
Example
# Create a list of numbersnumbers = [1, 2, 3, 4, 5]
# Create a list of stringsstrings = ["hello", "world", "python"]
# Create a list of listsnested_list = [[1, 2, 3], ["hello", "world"], ["python"]]
Must Check: Lists in Python
What is Dictionary in Python?
A dictionary is an unordered collection of data in the key: value pair form. The keys are unique, and the value can be of any data type. Like the lists, dictionaries are also mutable, i.e., you can change the key-value pair after creating the dictionary.
- To create a dictionary in Python, you have to use {}. : is used to separate the key and value.
- Elements in the dictionary can be accessed by their corresponding keys.
Example
# Create a dictionary of countries and capitalscapitals = { "USA": "Washington D.C.", "France": "Paris", "India": "New Delhi",}
Must Check: Dictionary in Python
Seek premier courses that prepare you for the job market after 12th. Also, pursue specialized online degree programs for a comprehensive career.
Key Differences Between List and Dictionary in Python
- A list is an ordered collection of items, whereas a dictionary is an unordered data collection in a key: value pair.
- Elements from the list can be accessed using the index, while the elements of the dictionary can be accessed using keys.
- The list allows duplicate items, while the dictionary does not allow any duplicate keys.
- A list can store any data type. In contrast, keys in the dictionary can be of any immutable data type, and values can be of any data type.
- Lists perform faster for ordered operations like sorting, while dictionaries perform faster for lookup operations.
Conclusion
Lists and Dictionaries are different data structures in Python. Lists are ordered collections of items, whereas the dictionary is an unordered collection of data in a key: value pair form. Both lists and dictionaries are mutable, i.e., you can add, edit, delete, or modify the elements.
In this article, we have briefly discussed the difference between lists and dictionaries in Python.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio