Python List and Dictionary

Python List is an ordered collection and we can access the elements using index (position of the element in the element).

Python Dictionary is un-ordered. The elements in the dictionary are not stored in a sequence.


Initialize a List in Python

To initialize a list in Python, we have to assign comma separated elements in squared brackets to a variable.

Access elements of List

To access elements of a list in Python, use index.

index starts with 0. aList[0] fetches first element of the list and aList[5] fetches 6th element in the list.

Output

Python Dictionary

To create a python dictionary, assign a variable with comma separated key:value pairs enclosed in curly braces.

In the following example, we create a dictionary with some initial key:value pairs.

Access Specific Key:Value Pair

To access a specific key:value pair in a dictionary, use key as index on the dictionary.

Output

Iterate through key:value pairs of Python Dictionary

To iterate through all key:value pairs of a Python Dictionary, you can use for loop as shown below.