You are currently viewing Difference Between List and Dictionary in Python

What is the difference between a list and a dictionary in Python? A list is a built-in data structure representing an ordered element collection. It is one of Python’s most commonly used data structures and provides data storage and manipulation flexibility. A dictionary is also a built-in data structure representing an unordered collection of key-value pairs. It is also known as an associative array or a hash map in other programming languages. Dictionaries provide an efficient way to store and retrieve data based on unique keys.

Advertisements

In this article, I will explain the list and dictionary and their differences with examples.

Related: You can easily convert a list to a dictionary at the same time you can also convert a dictionary value to a list.

1. Quick Examples of List vs Dictionary

If you are in a hurry, below are some quick examples of the list vs dictionary.


# Quick examples of list vs dictionary

# Example 1: Get elements from list by index
mylist = ['Python', 'Java', 'C++', 'JavaScript']
result = mylist[0]

# Example 2: Accessing last element in a list
last_element= mylist[-1]

# Example 3: Access elements of nested list by index
nested_list = [['Python', 'Java', 'C++'], 
    ['JavaScript', 'TypeScript', 'Java'], 
    ['Hadoop', 'Spark', 'Pega']]
print(nested_list[2][0])

# Example 4: Creating a Dictionary with string keys
technology = {'course':'python','fee': 4000,'duration':'60 days'}

# Example 5: Creating a Dictionary 
# with Mixed keys
technology = {'course':'python','fee': [2500,4000,3000]}

# Example 6: Creating a dictionary and accessing its value
# by using its corresponding keys
technology = {'course':'python','fee': 4000,'duration':'60 days'}
result = technology['fee']

2. Difference Between a List and a Dictionary

Following table shows the difference between the list and a dictionary.

List Dictionary
A list is an ordered collection of elements where each element is identified by its position or index. Lists are represented by square brackets [ ].A dictionary is an unordered collection of key-value pairs. Each element is identified by a unique key, and the values can be accessed using these keys. Dictionaries are represented by curly braces { }.
The elements in a list can be accessed using their indices. The index starts from 0 for the first element, and negative indices can be used to access elements from the end.The elements in a dictionary are accessed using their unique keys. Keys can be of any immutable type (e.g., strings, numbers, tuples). Dictionary values are not accessed using indices since the order of elements is not guaranteed.
Lists are mutable, meaning you can modify their elements even after creating them. You can change, add, or remove elements in a list.Dictionaries are mutable as well. You can modify the values associated with existing keys, add new key-value pairs, or remove key-value pairs from a dictionary.
Lists maintain the order of elements as they are inserted. The elements are accessed based on their indices, and the order of elements will be preserved.Dictionaries do not guarantee any specific order of elements. The order of elements may not be the same as the order in which they were inserted.
Lists can contain duplicate elements. Elements with the same value can appear multiple times in a list.Dictionary keys must be unique. If duplicate keys are used while creating a dictionary, only the last assigned value for a particular key will be stored.
Lists are suitable when you have an ordered collection of elements, and you want to access them based on their positions. They are useful for tasks like iterating, sorting, and manipulating sequences of data.Dictionaries are useful when you want to store and retrieve values based on specific keys. They are commonly used for mapping relationships between data, creating lookup tables, or storing data with meaningful associations.
Difference between a list and a dictionary

3. Difference Between Creation & Accessing Elements of List & Dictionary In Python

3.1 What is a List in Python?

Python list is an ordered collection of items that can contain elements of any data type, including other lists. It is a mutable data structure, meaning its contents can be modified after they have been created. In Python, Lists are typically used to store data that needs to be sorted, searched, or altered in some way. They can also be used to perform operations on a set of data quickly. In a list, the elements are enclosed in square brackets [] and separated by commas. Each element in a list can be of any data type, such as numbers, strings, booleans, lists, tuples, or even other complex objects.

You can initialize the list by using [] brackets and each element in the list is separated by ','(comma).


# Initialize the list
list_name = [element1, element2, element3, ...]

3.2 Accessing Elements of a List

You can access list elements using their corresponding index. List elements are indexed starting from 0, so the first element in a list has an index of 0, the second element has an index of 1, and so on.


# Syntax of list
mylist = ['Python', 'Java', 'C++', 'JavaScript']
print("List:", mylist)

# Get element from list by using index
result = mylist[0]
print("Get element from list by index:", result)

Yields below output.

python list dictionary

You can access elements from the ending side of the list, by using negative indexing, in which -1 represents the last element, -2 represents the second last, and so on.

Let’s use negative index and get the last element of a list.


# Accessing last elements in a list
last_element= mylist[-1]
print(last_element)

# Output
# 'Javascript'

 3.3 Access the Elements of a Nested List

In order to access the elements of a nested list, you need to use multiple sets of square brackets, with the first set referencing the outer list and the subsequent sets referencing the inner lists.


# Create nested list
nested_list = [['Python', 'Java', 'C++'], 
    ['JavaScript', 'TypeScript', 'Java'], 
    ['Hadoop', 'Spark', 'Pega']]

# Index 2 of the outer list 
# And Index 0 of the Inner list
print(nested_list[2][0])

# Output:
# Hadoop

3.4 What is a Dictionary in Python?

A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.

Moreover, dictionaries are mutable data types, which means that you can be added, deleted, and updated their key:value pairs.

A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary and it allows duplicates. On the other hand, keys are immutable types such as string, numbers(int, float), and tuple and it doesn’t allow duplicates. In case, you add duplicates, which will update the existing keys of the Python dictionary.


# Syntax of dictionary
dictionary_name = {key1: value1, key2: value2, key3: value3, ...}

3.5 Creating a Dictionary

You can create a dictionary named technology with string types keys and associated values. The dictionary contains information about a course, including the course name, fee, and duration. For example, the dictionary technology with its key-value pairs. The keys are strings 'course', 'fee', and 'duration', and the corresponding values are 'python‘, 4000, and '60 days', respectively.


# Creating a Dictionary 
technology = {'course':'python','fee': 4000,'duration':'60 days'}
print("The Dictionary Integer Keys:", technology)

Yields below output.

Python list dictionary

3.6 Accessing Dictionary Values

You can access dictionary elements by using its keys. Every key in a dictionary has its value so, you can use dict[key] in this notation to access the associated value of the specified key of a given dictionary.

Related: You can also get dictionary values as list and get dictionary keys as a list.


# Accessing dictionary elements by using 
# its corresponding keys 
print("Given Dictionary:", technology)
print("Get corresponding value of 'fee' :", technology['fee'])
print("Get corresponding value of 'course' :", technology['course'])
print("Get corresponding value of 'duration':", technology['duration'])

# Output:
# Given Dictionary: {'course': 'python', 'fee': 4000, 'duration': '60 days'}
# Get the corresponding value of 'fee' : 4000
# Get corresponding value of 'course' : python
# Get corresponding value of 'duration': 60 days

4. Conclusion

In this article, I have explained the difference between a list and a dictionary in Python. Also, learning, a list, and a dictionary are two different data structures that serve different purposes with examples.

Happy Learning !!