20 September 2025

Python - List, set and dictionary

A list is an ordered collection of items. The items can be approached with their index.
a = [3, 0, 5]
a:list[int] = [3, 0, 5]
A set is an unordered collection of unique items. You can assign the same item as many times as you want without error. It will appear just once in the set.
a = {3, 0, 5}
a:set[int] = {3, 0, 5}
A dictionary is an unordered collection of key-value pairs.
a = {3: 'E'}
a:dict[int, str] = {3: 'E', 0: 'G', 5: 'S'}
The :list[] etc. are just datatype suggestions for the IDE. They don't mean anything to the actual program, and you can still assign whatever you want to the variable.

No comments:

Post a Comment