top of page

Python Data Structures

This is the second module (Python Data Structures) for Python for Data Science, a course by IBM's Cognitive Class Labs.


- Lists and Tuples

- Sets

- Dictionaries


Lists and Tuples

Lists and tuples are compound data types


Ordered sequence

Separated by commas

Within a set of (parentheses)

Each element of a tuple can be accessed via an index or a negative index


Combine tuples by adding them, resulting in an index

We can slice the tuple


Obtain the length of a tuple

Tuples are immutable, meaning we can’t change them

  • Each variable references the same immutable tuple object

  • If we wanted to change the element at index 2, we couldn’t because the tuple is immutable

  • If we would like to manipulate a tuple, we must create a new tuple entirely


Sorts the tuple


A tuple can contain other tuples as well as other complex data types

Elements can be accessed using the standard indexing methods

Visualize it as a tree

Access deeper levels of the tree by adding another square bracket


Ordered sequence

Separated by commas

Lists are represented with [square brackets]

Can contain strings, floats, integers, and nest other lists or tuples

The same indexing conventions apply to lists (including a negative index that starts at -1, rather than 0)


We can slice the list to get the specified elements


Combine lists by adding them


Big difference between lists and tuples is that lists can be changed


This is a “method”

Concatenates a new list to the original list

Modifies the list by adding to it


Adds only one element to the list


Delete an element of the list


Convert a string to a list using split

Method split converts every group of characters separated by a space into an element of a list

Can use split function to separate strings on a specific character known as a delimiter


Multiple names refer to the same object

Changing an element in one list when both lists reference the same values will affect both lists


Duplicate the list without having to reference the same values in the new list




  • A type of collection

  • Can input different python types

  • Unordered

  • do not record element position

  • Only have unique elements

Define sets

  • Use curly brackets

  • Place elements of set within curly brackets

  • Duplicated items will not show


Convert list to a set using the function set

Use the list as the input to the function set

Result: list converted to a set

Set Operations

Can be used to change the set


Add an item to a set


Remove an item from a set


Verify if an item is in the set

Mathematical operations between sets


  • &

  • Causes all items that are not in both sets to disappear


New set of elements which contains all items in both sets


Check if a set is a subset



Dictionaries are a type of collection

A dictionary has keys and values


  • Denoted with curly brackets

  • The keys must be immutable and unique

  • Each key is followed by a value separated by a colon

  • Can be immutable, mutable, and duplicates

  • Each key and value pair is separated by a comma

Assign the dictionary to a variable

Use the key to look up a value

Use square brackets

Argument is the key, which outputs the value

Add a new entry

Delete an entry

Verify if an element is in the dictionary


Used to get a list of the keys


26 views0 comments

Recent Posts

See All


bottom of page