Lists & Dictionaries

Why does python make a reference to a list and not create a new variable?

But why does Python have this whole complicated reference system to begin with.

And will you have to consider list can be huge in our code right here.

It just has three integers that's only a few bytes of your computer's memory.

But say that this was I don't know 4 billion integers instead of just you know these three right here

Then it would be a huge problem to copy that entire list would all four billion values every time you

make a function call right here.

So as a default sort of shortcut it just stores this list once and instead just assigns a really cheap

and easy to handle list of reference to this variable.

And on that list reference gets copied because it's just a few bites and memory it just points to this

List continuation

You don't have to follow indentation when you are inside a list:

It's the same for \

List Recap

Dictionaries

Dictionaries don't have order.

Get

setdefault

Example of setdefault

import pprint

message = 'It was a bright cold day in APril, and the clocks were stricking.'
count = {}

for character in message.upper():
    count.setdefault(character, 0)
    count[character] = count[character] + 1

pprint.pprint(count)

Output:

{'I': 6, 'T': 4, ' ': 12, 'W': 2, 'A': 5, 'S': 3, 'B': 1, 'R': 4, 'G': 2, 'H': 2, 'C': 4, 'O': 2, 'L': 3, 'D': 3, 'Y': 1, 'N': 3, 'P': 1, ',': 1, 'E': 3, 'K': 2, '.': 1}

Last updated

Was this helpful?