Basics of Primary Data Structures in Python

Manjula Mishra
The Startup
Published in
5 min readDec 16, 2019

This post is a basic review of Python data structures. As I started going in depth by understanding the more complex things in Python, I realized that explaining the most basics things to non-programmers (specially recruiters) has become harder! This post is an attempt to define some of the most frequent and common Python-y things.

What is Python?

  1. Python is an open-source computer programming language which basically means that it’s freely available for anyone and everyone to use. It’s an interpreted, high level and general purpose programming language.
  2. Python is an object-oriented language : “Object-oriented programming (OOP) is a programming language model in which programs are organized around data, or objects, rather than functions and logic.”^1
  3. It has a very readable code format where indentation has a meaning (instead of “begin/end”)

What is Python Useful for?

Some of the main use cases are:

  1. Developing web applications.
  2. Web scrapping.
  3. Data analysis.
  4. Machine Learning/AI.
  5. Visualizations.

Basic Data Structures in Python:

We’ll be talking about: lists, tuples, dictionaries, sets, and strings.

Lists:

  • A list in Python is denoted by square brackets []. Lists are ordered list of elements inside the square brackets and separated by commas. The elements can be any type from integers to strings to real numbers. Lists can have lists, dictionaries inside (we call it nested lists: list of lists ).
  • lists are mutable that means the items inside a list can be deleted, more items can be added (append/delete). Thus, the length of a list is changeable.
  • The items/elements of a list can be accessed by index. Index, an integer value, is a particular position an element holds, like its address. The items of the list can be accessed either left to right or backwards, right to left. E.g. let’s create a list below:

example_list = [‘Ron’, 5, ‘Shark’, ‘boy’, 10, 15, 0]

Below is how indexing works in Python.

Indexing of a list: Left to Right and Right to left

The left to right indexing start at 0. If you type:

>>> example_list[0]

output: ‘Ron’

The same output can also be generated by:

>>> example_list[-6]

output: ‘Ron’

Running the same command in Google Colab

You can run for loops, while loops and/or conditionals on lists.

Tuples:

  • Tuples uses parenthesis () instead of brackets.
  • Tuples can have any number of items and any type of items (integers, real, string). They can also be nested by having list or tuples both inside a tuple.
  • Tuples are immutable, that means they can’t be modified (items can’t be added or deleted). The length of the tuple can’t be changed.
  • Tuples are useful to group together related data. E.g. Your name, address, age, school, date of birth etc.
  • The elements of a tuples can’t be modified directly. But if it’s a nested tuple which has a list inside, the items inside that list can be modified.
Since this tuple has a list inside, the item inside that list can be modified (in place of 6, it’s 9 now)
  • Same as lists, the items are accessed through indexing.

Dictionaries:

  • A dictionary uses curly braces {}. E.g. of a dict (dictionary)

my_dict = {“name”: “Mary”, “Age”: 21, “address”: “Palo Alto”}

  • A dictionary is used to store key value pairs.
  • A dictionary is an unordered collection of items. When you retrieve them, the key value pair might have changed position with the dictionary.
  • They keys of a dictionary must be unique.
  • Like lists, dictionaries are mutable. Its length can be changed by adding/deleting items from it.
  • Nested dictionary: A dictionary can contain anther dictionary or list or both as values.
  • Accessing dictionary items are different than accessing list or tuple items. Dictionary items are accessed via it’s keys.
Instead of positional indexing, you use ‘key’ to access the associated value.
  • The snapshot below shows how you access the keys of a dictionary using .keys() method, or they key value pair by .items() or just the values by .values() method.

Sets:

  • A set can be declared with curly braces {} or with set([]).
  • Like dictionaries , sets are also unordered.
  • Much like sets in math, elements of a set in Python are unique, thus no duplicates allowed.
Same as in math, a set will not have any duplicates.
  • The elements in a set can be of different data type like integers, float, string, None.
  • Since sets are immutable, a tuple, which is also immutable, may be included in a set.
  • Since lists and dicts are mutable types, they can’t be included in a set.
  • Elements of a set are immutable but the set itself is mutable/can be updated. That means the length of a set can be changed by adding another set to it.
The set1 is updated with by adding set2 to it. But it still has only unique values.
  • Items of a set can not be accessed using index because sets are unordered and thus they have no index. But you can loop over the set to see the items.
Accessing items of a set using a for loop.

Strings:

  • A string is a sequence of characters (a character is a symbol) and are denoted by either single quotes or double quotes interchangeably. E.g. ‘this is a string’ or “this is a string”.
  • Stings are immutable thus, they can’t be changed.
  • To access an element of the string, you use indexing (or reverse indexing i.e. right to left that we talked about earlier).
  • Once a string is assigned, the character of that string can’t be changed however, it can be reassigned to another string:

my_string = “I love math”

my_string = “I love Python”

New string got assigned the same name.

In this post, we explored a high level understanding of different Python data structure. The next post will talk about the basic operation that we can perform on lists, tuples, dictionaries, sets, and strings. I hope it’s helpful and concise. Thanks for reading! :-)

--

--