Python Data Structures: Lists, Dictionaries, Sets and Tuples
Organise data in Python: list operations, slicing and comprehensions, dictionaries for lookup and counting, sets for uniqueness and set algebra, tuples and unpacking, and how to choose the right structure. Every code example is executed and verified.
What you will learn
- Create, index, slice and modify lists and know which methods change them in place
- Write list and dictionary comprehensions
- Use dictionaries for lookup, counting and grouping
- Use sets to remove duplicates and combine collections with set operators
- Unpack tuples and return several values from a function
- Avoid aliasing and shallow-copy bugs and choose the right structure for a task
Prerequisites
- Python fundamentals: variables, conditions, loops and functions
Learning path
1 Lists
2 Dictionaries and sets
3 Tuples, copies and choosing the right structure
Practical project: A contact-book and word-statistics toolkit
- Contact book: store contacts as a dictionary from name to a record (a dictionary with
phone,cityand a list oftags). Write functionsadd_contact,find_by_city(returns names, sorted) andall_tags(returns the set of all tags used). - Word statistics: read a paragraph (type or paste one of at least 100 words). Count the words (lower case, without punctuation) with a dictionary; print the 5 most common words, the number of different words and the longest word.
- Set questions: ask two lists of tags from two people and print the shared tags, the tags only person A has, and the tags that exactly one person has.
- Tuples: write
min_max_avg(numbers)returning a tuple, and a function that returns the 3 contacts closest to a given (x, y) location, with locations stored as tuples. - Bug hunt: deliberately create two bugs (a shared default list and a shallow-copy problem), explain each in a comment and fix it.
- Write at least 10 test calls with expected results, including an empty input, and print a pass/fail summary.
References
Related courses
Python Fundamentals
Learn to program with Python from scratch: values and variables, decisions, loops, lists, dictionaries, strings, functions and error handling. Every…
JavaScript Fundamentals
The language of the web, from the ground up: values, types and operators, control flow, functions and arrow functions, arrays with…
SQL Fundamentals: Querying Relational Databases
Learn SQL with a realistic shop database: SELECT and WHERE, sorting, aggregate functions, GROUP BY and HAVING, joins, subqueries, and changing data…