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.


OUTLINE:

- Lists and Tuples

- Sets

- Dictionaries

Lists and Tuples

Lists and tuples are compound data types


1. TUPLES

Ordered sequence

Separated by commas

Within a set of (parentheses)

ree





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

ree








Concatenate

Combine tuples by adding them, resulting in an index

ree





We can slice the tuple

ree







len

Obtain the length of a tuple

ree



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

ree













sorted

Sorts the tuple

ree





nesting

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

ree











2. LISTS

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)

ree




Slicing

We can slice the list to get the specified elements

ree




Concatenate

Combine lists by adding them

ree




Mutable

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

ree







.extend

This is a “method”

Concatenates a new list to the original list

Modifies the list by adding to it

ree




.append

Adds only one element to the list

ree




del

Delete an element of the list

ree








.split

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

ree







Aliasing

Multiple names refer to the same object

ree








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

ree








Clone

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

ree












Sets


Sets

  • 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

ree

Type-casting

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

ree





Set Operations

Can be used to change the set


.add

Add an item to a set

ree

.remove

Remove an item from a set


ree

In

Verify if an item is in the set

ree




Mathematical operations between sets

Intersection

  • &

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

ree

.union

New set of elements which contains all items in both sets

ree

.issubset

Check if a set is a subset

ree



Dictionaries


Dictionaries are a type of collection

A dictionary has keys and values


ree










Dictionaries

  • 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

ree

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

ree

Add a new entry

ree






Delete an entry

ree




Verify if an element is in the dictionary

ree




.keys

Used to get a list of the keys

ree

Comments


Subscribe Form

©2019 by busybree. Proudly created with Wix.com

bottom of page