0% found this document useful (0 votes)
6 views4 pages

Python Lists & Dictionaries Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Python Lists & Dictionaries Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPREHENSIVE LECTURE NOTES ON PYTHON LISTS & DICTIONARIES

1. Introduction to Python Data Structures

Python provides several built-in data structures including lists, dictionaries, tuples, and sets. Lists and
dictionaries are among the most widely used because of their flexibility and power.

2. Python Lists

Definition:

A list is an ordered, mutable collection of items. Lists can store multiple data types.

Characteristics:

- Ordered

- Mutable

- Heterogeneous

- Dynamic

- Iterable

Creating Lists:

- numbers = [1, 2, 3]

- empty_list = []

- list_from_string = list("hello")

Indexing and Slicing:

- colors[0], colors[-1]

- a[1:4], a[:3], a[::2]

Operations:

- Concatenation: a + b

- Repetition: ["A"] * 3

- Membership: "apple" in fruits

- Length: len(list)
List Methods:

append, insert, extend, remove, pop, clear, sort, reverse, count, index, copy.

List Comprehension:

- [x*x for x in range(10)]

- [x for x in range(20) if x % 2 == 0]

Nested Lists:

matrix = [[1,2,3],[4,5,6],[7,8,9]]

Applications:

Storing datasets, tracking ordered items, stacks/queues, algorithms.

3. Python Dictionaries

Definition:

A dictionary stores key-value pairs. Keys must be unique and immutable.

Characteristics:

- Key-value mapping

- Mutable

- Fast lookup

- Values can be any type

Creating Dictionaries:

- student = {"name":"Aisha", "age":20}

- dict(name="Ali", age=30)

Accessing Items:

student["name"], [Link]("age")

[Link](), values(), items()

Operations:
Add/update: student["level"] = 300

Delete: pop, popitem, del, clear

Dictionary Methods:

copy, fromkeys, setdefault.

Nested Dictionaries:

records = {"001": {"name":"Tunde","score":85}}

Applications:

Structured data, JSON-like data, frequency counting, fast retrieval.

4. Differences Between Lists & Dictionaries

Lists: ordered, index-based, allow duplicates.

Dictionaries: key-based, keys unique, fast lookup.

5. Combining Lists & Dictionaries

- List of dictionaries: [{"name":"Ali"},{"name":"Musa"}]

- Dictionary of lists: {"math":[80,90], "eng":[70,75]}

6. Practical Examples

Word frequency counter, student grading system.

7. Common Errors

IndexError, KeyError, using mutable objects as dictionary keys.

8. Exercises

1. Create list of 10 numbers and print evens.

2. Create dictionary for library book.

3. Print diagonal of 3×3 matrix.

4. Count letter frequencies.

5. Student average scores.


6. Convert two lists into dictionary.

Conclusion:

Lists handle ordered sequences, dictionaries store structured key-value data. Both are essential for
Python programming.

You might also like