Data Structures

What is the difference between a list and a dictionary?

Both store collections of values, but they organise them differently. A list keeps items in order and you find them by their position. A dictionary stores pairs of a key and a value, and you find a value by using its key.

Comparison (in Python-style terms): 1) A list is like a shopping list: ["milk", "eggs", "bread"]. The first item is at position 0, the second at position 1, and so on. 2) A dictionary is like a phone book: {"Sara": "555-0101", "Omar": "555-0102"}. You look up "Sara" to find her number. 3) Use a list when order matters or you have many similar items. 4) Use a dictionary when you want to look things up by a name or ID. 5) Keys in a dictionary are usually unique, but values can repeat.

Many languages call dictionaries other names, such as maps, hash maps or objects.

Which would you use to store the names and ages of your classmates, and why?

Category