Collections in Python

Python is dynamically typed general purpose programming language. In this article I will present the most widely used methods and properties of the following Python collections:

Lists

Dictionaries

The code samples which I present here are working under Python 3.x. If you'd like read more about the difference between Python 2.x and 3.x you can check the Python documentation here.

Lists

In Python, lists are the most widely used collections. Lists are very similar to arrays in other programming languages. Because Python is a dynamic programming language different types of data can be stored inside lists, like strings, numbers (integers, floats), other lists, tuples and dictionaries.

Above, my_list_1 is a list with seven items, my_list_2 is a list with eleven elements. I used the range() method from Python to populate the second list. This can be very handy when a normal sequence of numbers has to be generated or when iterating over a number of times is needed:

# the my_list_3 will have 10 items – from 0 to 9 – and
# it will cause the for loop to iterate 10 times and print
# the value in nr variable
my_list_3 = range(10)
for nr in my_list_3:
print(nr)
# and maybe do some other stuff

Operations on Lists

The main operations which are used on lists are indexing, appending, adding (using + operator), extending, inserting and pop-ing items.

There are cases when a list needs to be extended, but the item needs to be added to a specific position within the list, then the insert() method should be used:

my_list = [2, 5, 6, 7]
my_list.insert(0, 1) # will insert the 1 at the 0th position
my_list.insert(2, 3) # will insert the 3 at the 2nd position
my_list.insert(3, 4) # will insert the 4 at the 3th position

In case an item should be removed from the list, the pop() and the remove() methods can help:

# pop - removes(pops out) the last item in the list
my_list = [1, 2, 3]
my_list.pop() # will remove and return the value 3
print(my_list) # will display [1, 2]
# remove - removes ONLY the first the value passed in as parameter from the list
my_list = [4, 5, 6, 7, 4, 6]
my_list.remove(6) # will only remove the 6 on the 2nd position
print(my_list) # will display [4, 5, 7, 4, 6]

Lists in python are very flexible and can be used as different type of data structures, like stack (LIFO) – using the append() and pop() methods – or queues(FIFO) – using the insert() and pop() methods.

Dictionaries

Dictionaries are key-value pairs in Python -- in other programming languages these can be called associative arrays. Dictionaries are indexed by keys. The dictionary type in Python is special because these are marked exactly the same way as objects in standard JSON format, { “key1”: “value1”, “key2”:value2, “key3”:”value3”}. The MongoDB Python API – pymongo – uses dictionaries when returning values and saving to the database.

The elements in a dictionary can be accessed using multiple methods, with indexing as seen above, and also with for loops:

# using a for loop to print out values
for key, val in enumerate(d):
print("{} = {}".format(key,val))
# the dictionary has a method called items()
# which does basically the same
for key, val in d.items():
print("{} = {}".format(key,val))

The values within a dictionary can be read and updated in the same way as in a list, using the index operator. The dictionary also has the get() method for accessing data and the update() method to change the data:

Dictionaries have length (the same way as lists have), this can be calculated using the len() method. Sorting, reversing does not make sense in case of dictionaries, so these are not available in Python.

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript.
I like technical writing and have good experience in creating tutorials and how to technical articles.
I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...