📕
SEIRFX
  • Introduction
  • About These Notes
  • Schedule
  • Unit 2
    • Node
      • Internet Fundamentals
      • Full-Stack Fundamentals
      • Intro to Node
      • Node Modules
      • Node Packages
    • Express
      • Intro to Express
      • Routes
      • Routes Lab
      • Views
      • Templates
      • Layouts & Controllers
    • CRUD & REST
      • GET & POST
      • GET & POST Lab
      • PUT & DELETE
    • API Calls in Express
      • Axios
      • Request (no longer maintained)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
  • Development Workflow
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
      • Objects Lesson
      • Objects quick guide
      • Object-ception
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Scope
      • Callbacks
      • Higher Order Functions
      • Callbacks Review Lab
      • Timing Functions
      • Iterators
      • Combining Data Types
      • Combining Data Types Lab
    • Javascript in the browser
      • DOM and Events
      • DOM Manipulation
      • DOM Review
      • DOM Review Lab
      • HP DOM Lab
      • Programmatic DOM Manipulation
      • Grids & Pyramids
      • DOM & Data
      • DOM Events
      • Color Palette Picker
      • Sketchpad
    • HTML5 Canvas
    • How To Reduce Redundancy
    • OOP
      • Westworld Lab
      • OOP Factories
      • OOP Inheritance
      • OOP Inheritance Lab
      • Tomagotchi Lab
      • OOP Space Battle
      • OOP Snowman
      • (2019) JavaScript OOP
      • (2016) OOP with Classes
      • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Left over Node/Express
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Being Successful in SEI
    • Internet Fundamentals
      • Internet Lab
    • Adjacent Workflow
      • UX/UI
      • Wireframing Exercise: Build an Idea
      • Agile
    • Post SEI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Learning Objectives
  • Sets
  • Enter the Set
  • Making a Set
  • Why Sets?
  • Important Note: Sets
  • Test it out
  • Adding to a Set
  • Removing from a Set
  • Tuples
  • Enter the Tuple
  • Why Tuples?
  • Tuple Syntax
  • Test it out
  • Quick Review: Sets, Tuples, Lists
  • Types
  • Additional Reading

Was this helpful?

  1. Python

Python Sets and Tuples

Learning Objectives

After this lesson, you will be able to:

  • Perform common actions with sets.

  • Perform common actions with tuples.

  • Know when to use different data structures.

Sets

We know that we can keep data in a list format.

unique_colors = ["red", "yellow", "red", "green", "red", "yellow"]
subscribed_emails = ["mary@gmail.com", "opal@gmail.com", "mary@gmail.com", "sayed@gmail.com"]

What might be the problem with these two examples?

In the first list, we have repetition in unique_colors section, making them not unique! The second example is more of a use case. If you wanted to keep track of subscribers by email, len(subscribed_emails) wouldn't give you an accurate number because of the duplicates.

Enter the Set

unique_colors_set = {"green", "yellow", "red"}
subscribed_emails_set = {"mary@gmail.com", "opal@gmail.com", "sayed@gmail.com"}

Notice the {} versus the [].

A list is a collection of elements, contained within square brackets [].

However, there is a specific version of a list called a set. What makes a set different is that all of the elements in a set must be unique. That is to say, nothing can appear more than once in a set. Sets are defined with curly braces {}.

A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set.

Making a Set

We can make a set directly as above, declaring it and placing the values in curly brackets rather than square brackets.

The other way to create a set is by using set() with a pre-existing list.

# my_set = set(a_list_to_convert)

unique_colors_list = ["red", "yellow", "red", "green", "red", "yellow"]
unique_colors_set = set(unique_colors_list)
# => {"green", "yellow", "red"}

Instead of passing a list into set(), we could just type a list directly in:

# my_set_2 = (["enter", "list", "here"])

unique_colors_set_2 = set(["red", "yellow", "red", "green", "red", "yellow"])
# => {"green", "yellow", "red"}

When making a set via a list Python removes duplicates automatically.

Why Sets?

The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set

Important Note: Sets

Lists are always in the same order:

  • my_list = ["green", "yellow", "red"] is always going to be["green", "yellow", "red"]

  • my_list[0] is always "green"; my_list[1] is always "yellow"; my_list[2] is always "red".

Sets are not! Like dictionaries, they're in any order.

  • my_set = {"green", "yellow", "red"} could later be {"red", "yellow", "green"}!

  • my_set[0] could be "green", "red", or "yellow" - we don't know!

We cannot do: print(my_set[0]) - it could be anything! Python won't let us.

Test it out

Let's pull up a new set_practice.py file and make some sets!

  • Make a list clothing_list containing the main color of your classmates' clothing.

  • Using clothing_list, make a set named clothing_set.

  • Use a for loop to print out both clothing_list and clothing_set.

  • Try to print an index!

Answers ```python clothing_list = ["red", "black", "brown", "black", "purple"] clothing_set = set(clothing_list) for item in clothing_list: print(item) for item in clothing_set: print(item) for i in range(len(clothing_set)): print(clothing_set[i]) # => TypeError: 'set' object does not support indexing ```

Adding to a Set

How do we add more to a set?

# In a list:
clothing_list.append("red")

# In a set
clothing_set.add("red")

Why is it append() in lists but add() in sets? It's because we can't guarantee it's going at the end!

Let's a few colors to clothing_list and clothing_set, then print them.

  • What happens if you add a duplicate?

Removing from a Set

Remember, lists are always the same order: ["green", "yellow", "red"].

  • my_list[0] is always "green".

Remember, sets are not!

  • With the set {"green", "yellow", "red"}, my_set[0] could be green, red, or yellow.

The same way, we need to be careful about removal:

# In a list:
clothing_list.pop() # Removes and returns the last item in the list.
clothing_list.pop(0) # Removes and returns a specific (here, the first) item in the list.

# In a set
clothing_set.pop() # No! This is unreliable! The order is arbitrary.
clothing_set.pop(0) # No! Python throws an error! You can't index sets.
clothing_set.remove('red') # Do this! Call the element directly!

Tuples

A set is a type of list which doesn't allow duplicates.

What if, instead, we have a list we don't want to change? What if we wanted that data to be immutable?

rainbow_colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]

We don't want:

rainbow_colors[0] = ("gray")
## Gray's not in the rainbow!
rainbow_colors.pop()
## We can't lose violet!
rainbow_colors.append("pink")
# Pink's not in the rainbow!

We want rainbow_colors to be immutable - the list cannot be changed.

How we do that in Python?

Enter the Tuple

Sets are one specific type of list.

  • No duplicates

  • Mutable.

  • Uses {}

Tuples are another specific type of list.

  • Duplicates

  • Immutable.

  • A list that cannot be changed.

  • Uses ()

Going back to the rainbow list we defined earlier. We want to make that a tuple:

rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")

Why Tuples?

Tuples are useful when you need data protection through immutability—when you never want to change the list. You can still access them through indexes (because the data never changes, including the position!)

Tuple Syntax

  • Created with parentheses ().

  • Access values via indices (like regular lists, but not like sets).

rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
print(rainbow_colors[1])
# Prints "orange"

Tuples can be printed with a for loop (just like a set or list!).

rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")

for color in rainbow_colors_tuple:
  print(color)

Test it out

  • Let's declare a tuple named seasons and set it to have the values fall, winter, spring, and summer.

  • We'll print the tuple and each value.

  • Then we'll try to reassign them.

Answers

```python seasons = ("fall", "winter", "spring", "summer") print(seasons) for season in seasons: print(season) seasons[0] = "autumn" # => TypeError: 'tuple' object does not support item assignment ```

Quick Review: Sets, Tuples, Lists

List:

  • The original, normal object: ["red", "red", "yellow", "green"].

  • Has duplicates; mutable: append(), insert(index), pop(), pop(index)

Set:

  • List without duplicates: {"red", "yellow", "green"}.

  • Mutable: add() and remove(element)

Tuple:

  • Has duplicates, but immutable: You can't change it!

  • ("red", "red", "yellow", "green") will always be ("red", "red", "yellow", "green").

Types

Variables certainly can hold a lot of different types of data! We've just been learning that sets, tuples, and lists are similar, but easily confused. How do we tell?

type() tells us what a variable is: set, tuple, list, dictionary, integer, string - anything!

Additional Reading

PreviousPython DictionariesNextPython Cheatsheet

Last updated 4 years ago

Was this helpful?

Repl.it that recaps Tuples
Python Count Occurrences of Letters, Words and Numbers in Strings and Lists-Video
Storing Multiple Values in Lists
Sets and Frozen Sets
Sets
Python Tuple
Tuples
Strings, Lists, Tuples, and Dictionaries Video
Python Data Structures: Lists, Tuples, Sets, and Dictionaries Video