dictionary vs list python with Examples

In this tutorial we will see what is list & tuples after that we will discuss about the difference between two important data type which is dictionary vs list python. Dictionary and list both are data types used for storing list of values using different method.

Dictionary in python?

 Dictionary  data types is as simple as placing as items inside a curly braces {} separated by commas. Where item has a key and a corresponding value that is expressed as a pair (key: value).

While the values can be of any data type and can repeat, keys must be of immutable type and must be unique.

# empty dictionary
my_dict = {}
# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'}
# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')]) 

List in python?

List data type is created by placing all the items (elements) inside square brackets [], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.).

List items are ordered, changeable, and allow duplicate values.

The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type.

Creating a list is as simple as putting different comma-separated values between square brackets. For example −

 # empty list
 my_list = []

 # list of integers
 my_list = [1, 2, 3]
 # list with mixed data types
 my_list = [1, "Hello", 3.4] 

Difference between Dictionary vs List python:

List is a collection of same or different types of values.Dictionary is a Collection of key and value pairs.
List is created by placing elements in [ ] seperated by commas “, “Dictionary is created by placing elements in { } as “key”:”value”, each key value pair is seperated by commas “, ”
The starting index of list are integers starting from 0.The keys of dictionary can be of any data type.
The elements are accessed by indices.The elements are accessed by key-values.
The order of the elements entered are maintained.There is no guarantee for maintaining order.
Was this article helpful?

Leave a Comment