Skip to content
GetITechSkills

Logic and Programming

Lists, Arrays and Dictionaries: Storing Collections of Data

Beginner · Concept · 6 min read · Updated October 30, 2026

One variable holds one value, but real data usually comes in groups. A to-do list has many tasks. A contact card has a name, a phone number and a city. A class has many students. Creating a separate variable for every item would be slow and, for data that grows, impossible. Programming languages solve this with collections, which are containers that hold many values under one name.

Two collections do most of the everyday work: the ordered list and the labeled dictionary. This entry shows how each one stores data, how to read and change it, and how to choose. The examples use a to-do list and a contact card, with Python as the main language.

Lists and arrays: an ordered row of items

A list keeps values in order. Each value has a position number, called an index. In most common languages, counting starts at zero, so the first item is at index 0, the second at index 1, and so on. In Python you write a list with square brackets:

tasks = ["buy stamps", "call dentist", "water plants"]

print(tasks[0])
print(tasks[-1])
print(len(tasks))

The output is:

buy stamps
water plants
3

tasks[0] is the first item. A negative index counts from the end, so tasks[-1] is the last one. The len function returns how many items the list holds, here 3.

Other languages use the word array for this idea. In JavaScript, const tasks = ["buy stamps", "call dentist", "water plants"]; gives an array, and tasks[0] reads the first item exactly as in Python. Strictly speaking, an array in languages like C or Java has a fixed size and holds one type of value, while a Python list can grow and mix types. For learning the basic idea, the two are the same.

Changing a list

Lists are mutable, which means you can change them after creating them:

tasks.append("pay rent")
tasks.remove("call dentist")
tasks[0] = "buy stamps and envelopes"
print(tasks)

append adds an item at the end, remove deletes the first item that matches the text, and assigning to an index replaces the item there. After these three steps the list is ['buy stamps and envelopes', 'water plants', 'pay rent']. You can also test for membership: "water plants" in tasks is True.

A slice takes a piece of the list. tasks[1:3] gives the items at index 1 and 2, here ['water plants', 'pay rent']. As with range, the end position is not included.

Looping through a list

Lists and loops belong together. To print a numbered to-do list:

for number, task in enumerate(tasks, start=1):
    print(number, task)

This prints 1 buy stamps and envelopes, then 2 water plants, then 3 pay rent. The enumerate function hands you the position and the item together, and start=1 makes the counting begin at 1, which is friendlier for people than 0.

Dictionaries: labeled values

A list finds things by position. That works when order matters, but it is awkward for a contact card. Is the phone number at index 1 or index 2? A dictionary solves this by storing key and value pairs. You look up a value by its key, which is a label you choose.

contact = {"name": "Maria Silva", "phone": "555-0100", "city": "Lisbon"}

print(contact["name"])
contact["email"] = "[email protected]"
print(contact)

The first print shows Maria Silva. The assignment adds a new key, email, and the second print shows all four pairs:

{'name': 'Maria Silva', 'phone': '555-0100', 'city': 'Lisbon', 'email': '[email protected]'}

Dictionaries are like a real paper dictionary or a phone book: you do not read from page one, you jump straight to the word you want. Other languages call the same idea a map, a hash map, an associative array or, in JavaScript, an object or a Map. Python keys must be values that cannot change, such as text, numbers or tuples of those. A list cannot be a key. Each key appears only once.

Looking up safely

If you ask for a key that is not there, Python stops with a KeyError. The get method is a gentler option:

print(contact.get("fax"))
print(contact.get("fax", "none"))

The first line prints None because no fax key exists. The second supplies a fallback and prints none. To check first, write "city" in contact, which is True here.

Looping through a dictionary

The items method gives each key with its value:

for key, value in contact.items():
    print(key, "->", value)

This prints one line per pair, such as name -> Maria Silva, in the order the keys were added. To remove a pair, use del contact["city"].

Choosing between a list and a dictionary

Question Better fit
Does the order of items matter? List
Will you mostly go through every item in turn? List
Do you need to find one item by a name or ID? Dictionary
Does each item have a clear label, like "phone"? Dictionary
Do you need several records of the same shape? List of dictionaries

The last row is very common. You can combine the two by putting dictionaries inside a list:

students = [{"name": "Ana", "score": 82}, {"name": "Ben", "score": 67}]
print(students[1]["name"])

Read it from left to right. students[1] picks the second dictionary in the list, and ["name"] picks its name, so the output is Ben. This list-of-records shape appears everywhere, from spreadsheets to the data that websites exchange.

Other collections you will meet

Python also has the tuple, which is like a list that cannot be changed, and the set, which keeps only unique values and ignores order. They are useful, but a list and a dictionary will carry you a long way.

Common mistakes

  • Counting from one. The first item is at index 0. Asking for tasks[3] in a three-item list fails, since the last valid index is 2.
  • Going past the end. Python raises an IndexError with the text "list index out of range". Check len() or loop over the items instead of guessing positions.
  • Asking for a missing key. contact["fax"] raises a KeyError. Use get or an in check when the key might not exist.
  • Mixing up square brackets and curly brackets. Square brackets make a list, and curly brackets with colons make a dictionary. An empty {} is an empty dictionary.
  • Changing a list while looping over it. Removing items from the list you are walking through can skip some. Build a new list for the result.
  • Expecting a copy when you assign. backup = tasks does not copy the list. Both names point to the same list, so a change through one shows up in the other. Use tasks.copy() for a separate copy.

When a collection error appears and you cannot see why, the habit of printing the collection and its length helps. The entry on how to debug code step by step turns this into a routine.

FAQ

Is an array the same as a list?

In everyday talk, yes. Technically an array is often fixed in size and holds one type, while a Python list is flexible. The choice of word usually depends on the language.

Is a dictionary the same as a JSON object?

They look alike. JSON is a text format for exchanging data, and its objects have text keys, and their values can be text, numbers, true or false, null, lists or other objects. These map naturally onto Python dictionaries. They are not the same thing, since one is a file format and the other lives in memory.

Which is faster, a list or a dictionary?

For finding one item by a key, a dictionary is built for quick lookups. A list has to be searched item by item unless you already know the index. For small amounts of data the difference does not matter.

Do dictionary keys keep their order?

In current versions of Python, a dictionary remembers the order in which keys were inserted. Do not depend on order in languages or situations where the documentation does not promise it.

Can a list hold different types?

In Python, yes. A list can hold numbers, text and even other lists. Mixing types makes the code harder to reason about, so most programs keep each list to one kind of item. More basics are on the Logic and Programming page.

Sources

  1. Data Structures (The Python Tutorial) (opens in a new tab) — Python Software Foundation (docs.python.org). Official chapter on lists, tuples, sets and dictionaries
  2. Indexed collections (JavaScript Guide) (opens in a new tab) — MDN Web Docs (Mozilla). Arrays in JavaScript
  3. Keyed collections (JavaScript Guide) (opens in a new tab) — MDN Web Docs (Mozilla). Map and Set, the closest JavaScript match to a Python dictionary

Next in Logic and Programming: How to Debug Code Step by Step

  • Logic and Programming

    Variables and Data Types Explained

    A variable is a named place to keep a value. Learn the main data types (numbers, text, true/false), how to convert between them, and common traps.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026

  • Logic and Programming

    Loops Explained: Repeating Work in Code

    Learn what for and while loops do, how to pick between them, how break and continue work, and how to avoid infinite loops, with a shopping cart example.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026

  • Logic and Programming

    How to Debug Code Step by Step

    A repeatable method for finding and fixing bugs: read the error, reproduce the problem, narrow it down, check values, fix one thing and confirm.

    • Beginner
    • Skill
    • 7 min read

    Updated October 30, 2026