Python Dictionary vs List (and Sets), with SEO Examples

About 30 minutes

What you need before this lesson
  • Variable
  • String
  • Integer
  • Float
  • Boolean
  • print
  • type
What this lesson adds
  • List
  • Dictionary
  • index
  • append
  • key
  • value
  • len
  • set

The counter at the top counts this lesson's exercises (8 of them). Reading and running the examples don't count.

In lesson 1 we worked with one value at a time, like url = "https://example.com/python-seo/" or clicks = 1540. But in SEO you almost never deal with one page or one keyword. You usually have 500 URLs, or 10,000 keywords, or 300 pages from Search Console, or thousands of rows from Screaming Frog. So we need a way to store a whole set of data instead of making a variable for everything. That's where the list comes in.

First: lists

Say you have 3 pages: https://example.com/, https://example.com/services/ and https://example.com/blog/. You could make a variable for each one (url_1, url_2, url_3), and that works for 3 pages. But if the site has 10,000 pages, you can't make url_1 all the way to url_10000. That's why we use a list:

Now urls is one variable, but it holds a set of values.

What a list looks like

A list goes between square brackets [ ], with a comma between the items:

And if we ask Python for the type:

Example
Expected output
<class 'list'>

Getting one item from a list

We have:

To get the first URL, we write:

Example
Expected output
https://example.com/

And here's something important to get used to from the start: Python counts from 0, not from 1. So urls[0] is the first item, urls[1] the second, and urls[2] the third.

Without running it, predict the output:

Predict the output

What will this line print?

If you guessed seo agency, remember that counting starts at zero: item 0 is seo agency, item 1 is seo company, and item 2 is seo services.

Getting the last item

You can also write keywords[-1], which means the last item, and keywords[-2], the item before the last. This is very handy when you have a big list and don't know exactly how many items are in it.

Predict the output

What will these two lines print?

How do I count the items?

With len(). In SEO you'll use this all the time, to find out how many URLs or how many keywords you have:

Predict the output

What will this code print?

Adding a new item

Your urls list has two pages, and you found a new one. You can add it with .append(). Think of it as saying: put this item at the end of the list.

Predict the output

What will print(urls) show after the append?

Does a list have to hold strings only?

No. status_codes = [200, 200, 404, 301] is a list of integers, and positions = [4.3, 8.7, 12.5] is a list of floats. Technically you can even mix types in one list, like data = ["https://example.com/", 200, True, 4.7]. But in real work, data like that gets confusing: you see True in the list, but what does it stand for? Indexable? Has a canonical? HTTPS? And that's exactly the problem a dictionary solves.

Second: dictionaries

We want to describe one page, with its URL, title, status code, clicks and whether it's indexable. We can write:

This is called a dict, or dictionary. The idea is simple: look at "status_code": 200. It has two parts. The first, "status_code", is the key, and the second, 200, is the value. A key leads to a value. Instead of seeing the number 200 and wondering what it means, you've said clearly that it's the status_code.

Getting a value from a dictionary

You want the URL:

Example
Expected output
https://example.com/python-seo/

And the number of clicks:

Example
Expected output
1540
Check your understanding

What's the difference between urls[0] and page["clicks"]?

Changing a value and adding a new one

If the page had "clicks": 1540 and now has 1800, we write page["clicks"] = 1800 and the value changes. And if you want to add new information, like the word count, you can write page["word_count"] = 1850 and it's added to the dictionary as is.

The key difference between a list and a dictionary

Remember it this way: a list fits when you have a set of similar things, like a set of keywords. A dictionary fits when you have one thing with several attributes, like one page with all its data.

What if I have 1,000 pages?

This is where we combine the two. A dictionary describes one page, and a list can hold a set of things, so we can write:

This is a very important point: pages itself is a list, but every item in it is a dict. So we have a list of dictionaries, and that's very close to the shape of the data we'll work with for the rest of the course.

To get the first page:

Example
Expected output
{'url': 'https://example.com/', 'status_code': 200}

Want the first page's URL? We know pages[0] gets the first dictionary, so we write pages[0]["url"]: get the first page, then get its URL.

A more important example. Without running it, predict the output:

Predict the output

What will the last line print?

Why? Because pages[1] means the second page, and then ["status_code"] means get its status code. This matters a lot, because soon, instead of this small sample, you might have 20,000 pages and Python checking them for you: get every 404 page, or every page with a missing title, or the pages with fewer than 300 words. That's where we go in the next lesson, when we learn conditions.

Third: sets

In SEO you'll often find yourself with duplicate URLs. Say you collected URLs from the sitemap, from Screaming Frog and from Search Console, and put them all in one list. You'll find the same URL more than once:

Example
Expected output
5

The list says 5, but we really have only 3 pages. Build any count or report on that number, and it comes out wrong.

This is where we use set(). A set is like a list in that it holds a group of values, with one important difference: no duplicates. Any value that appears more than once is kept only once.

Example
Expected output
3

Now we have the real number. But watch out for one thing: a set has no order. You can't write unique_urls[0] the way you would with a list, and if you print it, the items may come out in a different order from the one you wrote.

Without running it, predict the output:

Predict the output

What will these two lines print?

The most useful thing sets do in SEO: comparing

Say you have the URLs in your sitemap, and the URLs a crawler actually found as it went through the site. You want to know: are there pages in the sitemap the crawler never reached?

You can write a set directly between curly brackets { }, just like a dictionary, but without keys:

Example
Expected output
{'https://example.com/old-guide/'}

The - sign here means: give me what's in the first set and not in the second. So, a URL that's in the sitemap but the crawler didn't find. Why? Maybe no internal link points to it, or maybe the page was removed and the sitemap wasn't updated. It's not a verdict; it's a URL you need to look at.

And the other way round: pages the crawler found that aren't in the sitemap?

Example
Expected output
{'https://example.com/test-page/'}

On a site with 20,000 pages, doing this comparison by hand takes a whole day. With sets, it's one line. Remember it like this: a list when you want the group as it is, in order and with its duplicates; a set when you want each value once, or want to compare two groups.

Lesson exercise

We have 3 pages:

SEO challenge

Build a list of 3 dictionaries

Make a variable called pages that's a list of 3 dictionaries, one for each of the three pages above, with exactly these keys: url, title, status_code, indexable.

You still have exercises left

0/8

Exercises left: 8 of 8. The lesson counts as complete once you solve them all, and you can come back to them any time.

Well done, you reached the end of the lesson.