Chapter 5 (lists and dictionarys)

Lists

list = ['hello', 'wow', 78]

What is a list?

It is a mutable sequence of elements of any data type

What function you you use to add elements to the end of a list?

.append()

listName.append(what_you_want_to_add)

What function do you used to remove elements from and array?

.remove(elements_value_not_position)

list[:1] = 'jeez'

what would list now return?

['jeez', 'jeez', 78]

del list[1]

what would list now return?

['hello', 78]

Whats the point in tuples then

List out what makes tuples better than a list
  1. They are faster, because they are immutable this changes the way that they are stored
  1. They are perfect for data that doesn't change
  1. In some cases you must use a immutable variable
Define nested sequence

A nested sequence is a sequence that is stored within a sequence

Can you store a mutable list within a immutable tuple?

yes,

this is because when you store the list within the tuple, you are really storing its place in memory and not the actual list its self, this means that the list can still be changed

list = [('ben', 19), ('lew', 18)]

name, age = list[0]

what does name and age equal?

name = 'ben'

age = 19

for name, age in list:

Shared reference

list = ['cow', 'car', 'plane']

anotherlist = list

anotherlist[2] = 'grass'

print(list[2])

What does list[2] equal?

'grass'

Define shared reference

a shared reference is when you share the memory location across different variables

What could i do to get around this?

anotherlist = list[:]

Dictionarys

dictionary = {'key': 'value', 'key2': 'value2', 'key3': 'value3'}

Define a dictionary

A dictionary is a sequence made up of immutable keys and mutable values, you can call and edit values using the key

What is a item?

An item is a key and a value

dictionary['key2']

What would the code above be equal to?

'value2'

If the key is not found what happens?

we will receive an error

What does the .get() function do?

dictionary.get(key)

This is good because if the key doesn't exists then we get the value None

How do you create a new item?

dictionary['newkey'] = 'newValue'

How many values can you have per a key?

one

What data type can a value be?

any