📔
intro-to-python
  • An Introduction to Programming in Python (for Business Students)
  • exercises
    • Data Flow Diagramming Exercise
    • Developer Collaboration Exercise
    • README
    • "Web App" Exercise
      • checkpoints
        • Checkpoint 5: Bootstrap Layout
        • Checkpoint 4: Submitting Data from Web Forms
        • Checkpoint 3: Rendering HTML Pages
        • Checkpoint 1: Routing
        • Checkpoint 2: Modular Organization
      • "Web App" Exercise - Further Exploration
    • hello-world
      • "Hello World (Local)" Exercise
      • "Hello World (Local w/ Version Control)" Exercise
      • "Hello World (Colab)" Exercise
    • "Interface Capabilities" Exercise
    • "Continuous Integration 1, 2, 3" Exercise
    • "Web Service" Exercise
      • "Web Service" Exercise - Further Exploration
    • "Testing 1, 2, 3" Exercise
    • "Command-line Computing" Exercise
      • "Command-line Computing" Exercise
      • Professor Rossetti's Mac Terminal Configuration
      • Command-line Computing Exercise
    • "Codebase Cleanup" Assignment
    • "List Comprehensions" Exercise
    • "Groceries" Exercise
      • Python Datatypes (a.k.a. "Groceries") Exercise
      • Python Datatypes (a.k.a. "Groceries") Exercise
    • "Rock, Paper, Scissors" Exercise
      • "Rock, Paper, Scissors" Exercise
    • README
    • "Monthly Sales Predictions" Exercise
    • Setting up your Local Development Environment
    • "Chart Gallery" Exercise
    • "Run the App" Exercise
    • "Web Requests" Exercise
    • "API Client" Exercise
    • "Custom Functions" Exercise
    • Process Diagramming Exercise
  • notes
    • python
      • packages
        • The bigquery Package
        • The PySimpleGUI Package
        • The dotenv Package
        • The matplotlib Package
        • The requests Package
        • The altair Package
        • The gspread Package
        • The PyMySQL Package
        • The psycopg2 Package
        • The selenium Package
        • The seaborn Package
        • The pytest Package
        • The SpeechRecognition Package
        • The flask Package
        • The pandas Package
        • The spotipy Package
        • The pipenv Package
        • The nltk Package
        • The sqlalchemy Package
        • The pymongo Package
        • The plotly Package
        • The BeautifulSoup Package
        • The sendgrid Package
        • The fpdf Package
        • The autopep8 Package
        • The tweepy Package
        • The twilio Package
        • The tkinter Package
      • Python Datatypes Overview
        • Numbers
        • Classes
        • Dates and Times
        • Strings
        • None
        • Dictionaries
        • Booleans
        • Lists
        • Class Inheritance
      • Control Flow
      • Python Modules
        • The webbrowser Module
        • The time Module
        • The csv Module
        • The sqlite3 Module
        • The itertools Module
        • The json Module
        • The math Module
        • The os Module
        • The statistics Module
        • The random Module
        • The pprint Module
        • The datetime Module
        • The collections Module
      • Printing and Logging
      • Comments
      • Syntax and Style
      • Functions
      • Variables
      • Errors
      • Docstrings
      • File Management
      • User Inputs
      • Debugging
    • clis
      • The git Utility
      • Heroku, and the heroku Utility
      • Anaconda
      • The chromedriver Utility
      • The brew Utility (Mac OS)
      • The pdftotext Utility
      • The python Utility
      • The pip Utility
    • Software
      • Software Licensing
      • Software Documentation
      • Software Ethics
      • Software Testing Overview
      • Application Programming Interfaces (APIs)
      • Software Version Control
      • Software Refactoring Overview
    • devtools
      • The VS Code Text Editor
      • Code Climate
      • Travis CI
      • GitHub Desktop Software
      • Git Bash
      • Google Colab
    • Information Systems
      • Computer Networks
      • Processes
      • Datastores
      • Information Security and Privacy
      • People
    • Technology Project Management
      • Project Management Tools and Techniques
      • The Systems Development Lifecycle (SDLC)
    • hardware
      • Servers
    • Environment Variables
  • projects
    • "Executive Dashboard" Project
      • testing
      • "Exec Dash" Further Exploration Challenges
    • The Self-Directed (a.k.a "Freestyle") Project
      • "Freestyle" Project - Demonstration
      • "Freestyle" Project - Implementation (TECH 2335 Version)
      • "Freestyle" Project - Implementation
      • "Freestyle" Project Proposal
      • plan
    • "Robo Advisor" Project
      • Robo Advisor Project - Automated Testing Challenges
      • "Robo Advisor" Further Exploration Challenges
    • "Shopping Cart" Project
      • "Shopping Cart" Project - Automated Testing Challenges
      • "Shopping Cart" Further Exploration Challenges
      • "Shopping Cart" Project Checkpoints
  • License
  • Exam Prep
  • units
    • Unit 4B: User Interfaces and Experiences (Bonus Material)
    • Unit 5b: Databases and Datastores
    • Module 1 Review
    • Unit 7b: Processing Data from the Internet (Bonus Material)
    • Unit 9: Software Products and Services
    • Unit 8: Software Maintenance and Quality Control
    • Unit 7: Processing Data from the Internet
    • Unit 6: Data Visualization
    • Unit 5: Processing CSV Data
    • Unit 4: User Interfaces and Experiences
    • Unit 3: Python Datatypes
    • Unit 12: Project Presentations
    • Unit 2: Python Language Overview
    • Unit 11: Project Implementation Sprint
    • Unit 1: The Python Development Environment
    • Unit 10: Software Planning, Analysis, and Design
    • Unit 0: Onboarding
    • Unit 5B: Advanced Data Analytics
  • Contributor's Guide
Powered by GitBook
On this page
  • Sorting
  • Iteration
  • Mapping
  • Filtering
  • Grouping

Was this helpful?

  1. notes
  2. python
  3. Python Datatypes Overview

Lists

PreviousBooleansNextClass Inheritance

Last updated 4 years ago

Was this helpful?

Reference:

A List represents a numbered, ordered collection of items. A List may contain zero or more items. A list can contain items of any datatype, but as a best practice, all items in a list should share a datatype and structure:

# DO:

[]

[1, 2, 3, 4]

[100, 75, 33]

["fun", "times", "right?"]

[{"a": 1, "b": 2}, {"a": 5, "b": 6}] # lists can contain dictionaries

[[1, 2, 3], [4, 5, 6], [7, 8, 9]] # lists can be "nested" inside other lists

# DON'T:

[100, "fun"] # mixed datatypes

[{"a": 1, "b": 2}, {"x": 5, "z": 6}] # non-standard dictionary keys

Like other languages, individual list items can be accessed by their index. List item indices are zero-based, meaning the index of the first list item is 0.

arr = ["a", "b", "c", "d"]
arr[0] #> "a"
arr[1] #> "b"
arr[2] #> "c"
arr[3] #> "d"
arr[4] #> IndexError: list index out of range

arr.index("a") #> 0
arr.index("b") #> 1
arr.index("c") #> 2
arr.index("z") #> -1 (applies to any item not found in the list)

Equality operators apply:

[1,2,3] == [1,2,3] #> True
[1,2,3] == [3,2,1] #> False

Inclusion operators apply:

arr = [1,2,3,4,5]

3 in arr #> True

3 not in arr #> False

Common list functions and operators include the following built-in Python functions:

arr = [6,3,9,7]

len(arr) #> 4 (counts number of items in the list)

min(arr) #> 3 (minimum value in the list)

max(arr) #> 9 (maximum value in the list)

Add an element to the end of a list:

arr = ["a", "b", "c", "d"]
arr.append("e") # this is a mutating operation
arr #> ["a", "b", "c", "d", "e"]

Remove an element from a list by specifying the index of the item you would like to remove:

arr = ["a", "b", "c", "d"]
del arr[1] # this is a mutating operation
arr #> ['a', 'c', 'd']

Concatenate two lists:

arr = ["a", "b", "c", "d"]
arr2 = ["x", "y", "z"]
arr3 = arr + arr2
arr3 #> ["a", "b", "c", "d", "x", "y", "z"]

Remove duplicate values in a list by converting it to another datatype called a "Set" (which rejects non-unique values), and then converting it back to a "List":

arr = [1,2,2,2,3]
arr2 = list(set(arr))
arr2 #> [1, 2, 3]

list(set(["hello", "hello", "hello"])) #> ['hello']

Sorting

Sort a list:

arr = [6,3,8]
arr.sort() # this is mutating
arr #> [3, 6, 8]

arr.reverse() # this is mutating
arr #> [8, 6, 3]

If you have a list of dictionaries, you should be able to sort it based on dictionary values:

teams = [
    {"city": "New York", "name": "Yankees"},
    {"city": "New York", "name": "Mets"},
    {"city": "Boston", "name": "Red Sox"},
    {"city": "New Haven", "name": "Ravens"}
]

def team_name(team):
    return team["name"]

def sort_by_hometown(team):
    return team["city"]

def sort_special(team):
    return team["city"] + "-" + team["name"]

teams2 = sorted(teams, key=team_name)
teams2 #> [{'city': 'New York', 'name': 'Mets'}, {'city': 'New Haven', 'name': 'Ravens'}, {'city': 'Boston', 'name': 'Red Sox'}, {'city': 'New York', 'name': 'Yankees'}]

teams3 = sorted(teams, key=sort_by_hometown)
teams3 #> [{'city': 'Boston', 'name': 'Red Sox'}, {'city': 'New Haven', 'name': 'Ravens'}, {'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]

teams4 = sorted(teams, key=sort_special)
teams4 #> [{'city': 'Boston', 'name': 'Red Sox'}, {'city': 'New Haven', 'name': 'Ravens'}, {'city': 'New York', 'name': 'Mets'}, {'city': 'New York', 'name': 'Yankees'}]
import operator

teams = [
    {"city": "New York", "name": "Yankees"},
    {"city": "New York", "name": "Mets"},
    {"city": "Boston", "name": "Red Sox"},
    {"city": "New Haven", "name": "Ravens"}
]

teams = sorted(teams, key=operator.itemgetter('city'))
teams #> [{'city': 'Boston', 'name': 'Red Sox'}, {'city': 'New Haven', 'name': 'Ravens'}, {'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]

Iteration

Reference:

A list can be iterated, or "looped" using a for ... in ... statement:

for letter in ["a", "b", "c", "d"]:
    print(letter)

#> a
#> b
#> c
#> d

TIP: If it helps, you can vocalize this like "for each item in the list of items, do something with that item"

A common pattern is to loop through one list to populate the contents of another:

arr = [1, 2, 3, 4]
arr2 = []

for i in arr:
  arr2.append(i * 100)

arr #> [1, 2, 3, 4]
arr2 #> [100, 200, 300, 400]

Mapping

Lists can be looped "in-place" using Python's built-in map() function. The map() function takes two parameters. The first parameter is the name of a pre-defined function to perform on each item in the list. The function should accept a single parameter representing a single list item. The second parameter is the actual list to be operated on:

arr = [1, 2, 3, 4]

def enlarge(num):
    return num * 100

arr2 = map(enlarge, arr)
arr2 #> <map object at 0x10c62e710>
list(arr2) #> [100, 200, 300, 400]

NOTE: remember to use the return keyword in your mapping function!

Another way of mapping is to use a list comprehension:

arr = [1, 2, 3, 4]

[i * 100 for i in arr] #> [100, 200, 300, 400]

teams = [
    {"city": "New York", "name": "Yankees"},
    {"city": "New York", "name": "Mets"},
    {"city": "Boston", "name": "Red Sox"},
    {"city": "New Haven", "name": "Ravens"}
]

[team["name"] for team in teams] #> ['Yankees', 'Mets', 'Red Sox', 'Ravens']

Filtering

Use the filter() function to select a subset of items from a list - only those items matching a given condition. The filter function accepts the same parameters as the map() fuction:

arr = [1,2,4,8,16]

def all_of_them(i):
    return True # same as ... return i == i

def equals_two(i):
    return i == 2

def greater_than_two(i):
    return i > 2

def really_big(i):
    return i > 102

filter(all_of_them, arr) #> <filter at 0x103fa71d0>
list(filter(all_of_them, arr)) #> [1, 2, 4, 8, 16]
list(filter(equals_two, arr)) #> [2]
list(filter(greater_than_two, arr)) #> [4, 8, 16]
list(filter(really_big, arr)) #> []

Note: depending on how many items matched the filter condition, the resulting filtered list may be empty, or it may contain one item, or it may contain multiple items

When using the filter function, observe this alternative filtering syntax involving the keyword lambda:

arr = [1,2,4,8,16]
list(filter(lambda i: i > 2, arr)) #> [4, 8, 16]
teams = [
    {"city": "New York", "name": "Yankees"},
    {"city": "New York", "name": "Mets"},
    {"city": "Boston", "name": "Red Sox"},
    {"city": "New Haven", "name": "Ravens"}
]

def yanks(obj):
    return obj["name"] == "Yankees"

def from_new_york(obj):
    return obj["city"] == "New York"

def from_new_haven(obj):
    return obj["city"] == "New Haven"

def from_new_something(obj):
    return "New" in obj["city"]

list(filter(yanks, teams)) #> [{...}]
list(filter(from_new_york, teams)) #> [{...}, {...}]
list(filter(from_new_haven, teams)) #> [{...}]
list(filter(from_new_something, teams)) #> [{...}, {...}, {...}]

If you need to implement complex filtering conditions, consider using a list comprehension, or "lambda" syntax, or consider writing out your function the long way:

teams = [
    {"city": "New York", "name": "Yankees"},
    {"city": "New York", "name": "Mets"},
    {"city": "Boston", "name": "Red Sox"},
    {"city": "New Haven", "name": "Ravens"}
]

# using a list comprehension
def teams_from(city):
  return [team for team in teams if team["city"] == city]

# using "lambda" syntax
def teams_from2(city):
  return list(filter(lambda team: team["city"] == city, teams))

# the long way
def teams_from3(city):
  matches = []
  for team in teams:
      if team["city"].upper() == city.upper():
          matches.append(team)
  return matches

print(teams_from("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]
print(teams_from2("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]
print(teams_from3("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]

Grouping

Alternatively for simple attribute-based sorting, you could use the itemgetter() function, for example:

Reference: .

If your list is full of , you can filter() based on their attribute values:

Reference the for additional operations.

https://docs.python.org/3/library/stdtypes.html#lists
https://docs.python.org/3/tutorial/datastructures.html?highlight=lists#more-on-lists
operator module's
https://docs.python.org/3/tutorial/datastructures.html?#list-comprehensions
https://docs.python.org/3/tutorial/datastructures.html?highlight=lists#list-comprehensions
https://docs.python.org/3/tutorial/controlflow.html#for-statements
https://docs.python.org/3/library/functions.html#filter
dictionaries
itertools module