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)






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









Concatenate

Combine tuples by adding them, resulting in an index






We can slice the tuple








len

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














sorted

Sorts the tuple






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












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)





Slicing

We can slice the list to get the specified elements





Concatenate

Combine lists by adding them





Mutable

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








.extend

This is a “method”

Concatenates a new list to the original list

Modifies the list by adding to it





.append

Adds only one element to the list





del

Delete an element of the list









.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








Aliasing

Multiple names refer to the same object









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









Clone

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













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


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






Set Operations

Can be used to change the set


.add

Add an item to a set


.remove

Remove an item from a set



In

Verify if an item is in the set





Mathematical operations between sets

Intersection

  • &

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


.union

New set of elements which contains all items in both sets


.issubset

Check if a set is a subset




Dictionaries


Dictionaries are a type of collection

A dictionary has keys and values












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


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





.keys

Used to get a list of the keys


13 views

©2019 by busybree. Proudly created with Wix.com