LearnFrenzy
Sign in Take a free test

Working with Data · Lesson 5 of 6

Python Lists and Dictionaries: The Two Structures You Will Use Every Day

Store many values in a list, look things up instantly with a dictionary, and combine both to model real data — with list comprehensions explained simply.

12 min read Beginner Updated Nov 5, 2026 Reviewed by Saurabh Samir

Almost every real Python program stores collections of data. Two structures do most of the work: lists for ordered items and dictionaries for looking things up by a key.

Lists: ordered and changeable

marks = [78, 91, 64, 88]

marks.append(95)        # add to the end
marks.insert(0, 70)     # add at position 0
marks.remove(64)        # remove the first 64
top = max(marks)        # 95
marks.sort(reverse=True)
print(marks)            # [95, 91, 88, 78, 70]
print(marks[0], marks[-1], len(marks))

Lists can hold any type, even other lists. Indexing and slicing work exactly like strings, but unlike strings, lists are mutable — you can change them in place.

List comprehensions: a loop in one line

squares = [n * n for n in range(1, 6)]            # [1, 4, 9, 16, 25]
passed  = [m for m in marks if m >= 75]            # filter
names   = [s.title() for s in ["asha", "ravi"]]    # transform

Read it as: "give me n * n for each n in the range". Use them for short, simple transformations; write a normal loop when the logic gets complicated.

Dictionaries: look up by key

student = {
    "name": "Meera",
    "branch": "ECE",
    "cgpa": 8.7,
}

print(student["name"])            # Meera
student["cgpa"] = 8.9             # update
student["city"] = "Chennai"       # add a new key
print(student.get("phone", "NA")) # NA — safe lookup, no error

Use .get() when a key might be missing. student["phone"] would raise a KeyError and crash the program.

Looping over a dictionary

for key, value in student.items():
    print(f"{key}: {value}")

Counting with a dictionary — a placement favourite

text = "data science and data analytics"
counts = {}
for word in text.split():
    counts[word] = counts.get(word, 0) + 1

print(counts)   # {'data': 2, 'science': 1, 'and': 1, 'analytics': 1}

This "count frequency" pattern appears in a surprising number of coding rounds — characters in a string, votes, repeated elements. Learn it by heart.

Combining both: a list of dictionaries

batch = [
    {"name": "Meera", "cgpa": 8.9},
    {"name": "Kabir", "cgpa": 7.4},
    {"name": "Sana",  "cgpa": 9.1},
]

eligible = [s["name"] for s in batch if s["cgpa"] >= 8]
ranked = sorted(batch, key=lambda s: s["cgpa"], reverse=True)
print(eligible)            # ['Meera', 'Sana']
print(ranked[0]["name"])   # Sana

This is exactly how data from an API or a database usually looks in Python — so this small example is closer to real work than it seems.

Quick comparison

ListTupleSetDictionary
Syntax[1, 2](1, 2){1, 2}{"a": 1}
OrderedYesYesNoYes (insertion order)
ChangeableYesNoYesYes
DuplicatesAllowedAllowedNot allowedKeys unique

Try it yourself: Store 5 students as a list of dictionaries (name, branch, cgpa). Print the average CGPA, the topper, and how many students are in each branch (use the counting pattern).

Common interview questions

Try answering each one aloud before you open it.

1What is the difference between a list and a tuple?

Lists are mutable and use square brackets; tuples are immutable and use parentheses. Tuples are slightly faster, can be dictionary keys, and signal that the data should not change.

2How do you avoid a KeyError in a dictionary?

Use dict.get(key, default), check with "if key in dict", or use collections.defaultdict.

3What is a list comprehension?

A concise way to create a list from an iterable, optionally with a condition — for example [x*x for x in nums if x > 0].

Practise this

Build something with it

FAQs

Are dictionaries ordered?

Yes, since Python 3.7 dictionaries keep the order in which keys were inserted.

When should I use a set?

When you need unique items or fast membership checks — for example removing duplicates from a list with set(my_list).

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links