Coding Tier 1: problem solving in Python · Chapter 2
Lists, tuples, sets and dictionaries
About this chapter
Many Python interview problems are decided by which container holds the data. This chapter works through one small program, a library returns desk that reads a line for each returned book, and uses it to compare lists, tuples, sets and dictionaries, then the extra containers in the standard library, how strings behave, and when a comprehension reads better than a loop.
Who it is for: Students who can write loops and functions in Python and want to pick the right container for a problem and explain the choice in an interview.
You will be able to
- Choose between a list, a tuple, a set and a dictionary for a given job and justify the choice.
- Explain why asking whether a value is present is a different kind of operation on a set than on a list.
- Use Counter, defaultdict and deque from the standard library where the basic types are awkward.
- Explain why a string cannot be edited in place and what its methods really return.
- Rewrite a simple loop as a comprehension and say when not to.
The key idea: The container is part of the algorithm. The same question can mean a slow scan through every item in one structure and a direct lookup in another, so choosing the data structure is often most of the solution.
In this chapter
- A slip as data
- Sets and dictionaries
- The collections module
- Strings
- Comprehensions
Locked: needs a plan
You can read chapter 1 free. Chapter 2 is the next one.
What you can do now, free: re-read chapter 1 and try its problems.
What a plan gives you: all chapters of the track you pick and their checks. Starter is ₹229 a month or ₹1,699 a year. It renews until you cancel, and you can cancel any time. Paid plans open at launch.
Why is this locked?
Chapter 1 of every track is free, so you can see how the track reads. The rest of a track and its checks come with a plan, or open as you pass each chapter's check.
The price is for one period, a month or a year. A plan renews until you cancel.