Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views3 pages

Understanding Lists and Dictionaries in Python

Uploaded by

willjuru
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)
9 views3 pages

Understanding Lists and Dictionaries in Python

Uploaded by

willjuru
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
You are on page 1/ 3

Understanding Lists and Dictionaries in Python

In Python, lists and dictionaries are two of the most common data structures. They help you
store and organize information, but in different ways.

What is a List?
A list is a collection of items stored in a specific order. You can add, remove, or change items in a
list.

Analogy: Think of a list like a shopping list — it has items in order: ['milk', 'bread', 'eggs'].

Example in Python:
shopping_list = ['milk', 'bread', 'eggs'] print(shopping_list[0]) # prints 'milk'

What is a Dictionary?
A dictionary stores data in key-value pairs. Each item has a unique key and a value associated
with it.

Analogy: Think of a dictionary like a real-life dictionary or a contact list — you look up a word
(key) to get its definition (value).

Example in Python:
person = {'name': 'Alice', 'age': 25, 'city': 'Kigali'} print(person['name']) # prints 'Alice'
Differences Between Lists and Dictionaries

Feature List Dictionary

Structure Ordered collection of items Unordered collection of key-value pairs

Access By index (e.g. list[0]) By key (e.g. dict['name'])

Example ['apple', 'banana'] {'fruit': 'apple', 'color': 'red'}

Use Case When order matter or you only need aWhen each value needs a unique name or label
values

When to Use Lists vs. Dictionaries


- Use a list when the order of items is important, or when you're working with a simple collection of
values.
- Use a dictionary when you want to label each piece of data with a unique key or when you
need to quickly look up values by name.

Practice Exercises
1. Create a dictionary about yourself with these keys: name, age, hobby, and print the whole dictionary.​
2. Add a new food to your list and print the updated list.

3 Create a dictionary for a student with keys: 'name', 'age', 'grade'. Fill in your own values.

4. Print the student's name from the dictionary.

5. Change the student's grade and print the updated dictionary.

You might also like