Difference between List and Dictionary data types in Python

In Python, lists and dictionaries are both used to store collections of data, but they have some key differences.

LIST

A list is an ordered collection of elements, where each element can be accessed by its index.

LIST

Lists are created using square brackets []

my_list = [1, 2, 3, 'four', 5.0]

LIST

Lists are created using square brackets [] and elements are separated by commas

my_list = [1, 2, 3, 'four', 5.0]

Dictionary

A dictionary, on the other hand, is an unordered collection of key-value pairs, where each value can be accessed by its key

Dictionary

Dictionaries are created using curly braces {} and key-value pairs are separated by commas. For example:

my_dict = {'apple': 1, 'banana': 2, 'orange': 3}

Dic Usage

my_dict is a dictionary that maps strings to integers. Values can be accessed using their keys:

print(my_dict['apple'])   # prints 1 print(my_dict['orange'])  # prints 3

List and Dic

Important difference is that lists are mutable, which means that you can change their elements after they have been created, while dictionaries are also mutable, but you can change their values by their keys.