In this lesson you will
- Create, index, slice, sort and transform lists
- Read, update and loop over dictionaries safely with get() and items()
- Remove duplicates and compare groups with sets
Lists: ordered and changeable
A list is written with square brackets and commas, and it can hold any type, even other lists. Positions start at 0, so cities[0] is the first item, and negative positions count from the end, so cities[-1] is the last. append() adds to the end and insert(0, ...) adds at the front, which is why Riyadh moved to position 0. A slice such as cities[1:3] copies positions 1 and 2; like range(), the stop position is not included. Asking for a position that does not exist, such as cities[10], raises an IndexError.
sort() changes the list in place and returns nothing, so write prices.sort() on its own line, not prices = prices.sort(), which would leave None. If you need a sorted copy and want the original kept, use sorted(prices) instead. sum(), max(), min() and len() work on any list of numbers. The last line is a list comprehension: [round(p * 1.05, 2) for p in prices] builds a new list by running the expression once for every item, which is the short form of a for loop that appends. Notice that 12.5 became 13.12, not 13.13: as lesson one showed, floats are stored in binary and round() follows the stored value, so use the decimal module where every fils matters.
Console
- Console messages, errors, form data and clicked links appear here.
This printed ['Riyadh', 'Dubai', 'Muscat', 'Doha', 'Manama'], then Riyadh Manama 5, then ['Dubai', 'Muscat']. The prices became [12.5, 30, 45, 99] 186.5 99 and the VAT list [13.12, 31.5, 47.25, 103.95].
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between a list and a tuple?
A tuple is written with round brackets, such as (25.2, 55.3), and cannot be changed after it is made. Use it for fixed groups such as coordinates or a pair returned by a function, and a list for collections that grow or change.
Are Python dictionaries the same as JSON?
They look alike but are not the same. JSON is text with double quotes only, true, false and null, while Python uses True, False and None. The json module converts between them with json.loads() and json.dumps().
How do I remove an item from a list or a dictionary?
For a list, items.remove("Doha") removes the first matching value and items.pop() removes and returns the last item. For a dictionary, d.pop("key") removes a key and returns its value, and del d["key"] removes it without returning anything.
Can a list hold different types at once?
Yes, ["Dubai", 3, True] is valid Python. In practice, keep one kind of item per list, such as all prices or all product dictionaries, so loops and functions such as sum() work on every item.
Why did my set print in a different order?
Sets are stored for fast lookup, not in insertion order, and the order of strings can change between runs. If you need a predictable order, print sorted(my_set), or keep a list as well.
Sources
Facts in this lesson were checked against these official pages on 2 December 2026.
- Python docs: Data Structures
- Python docs: Lists in the tutorial
- Python docs: Mapping Types, dict
- Python docs: Set Types
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.