LISTS
DICTIONARIES
100

nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']

print nameList([1][-1])


k will be returned by print statement.

100

>>> spam = {'name': 'Zophie', 'age': 7}

>>> spam['color']


Trying to access a key that does not exist in a dictionary will result in a KeyError error message

200

Slicing

What we call the operation which is used to extract particular range from a sequence.

200

>>> spam = {'color': 'red', 'age': 42}

>>> for v in spam.values():

print(v)

It will return 

red

42

300

>>> spam = ['cat', 'bat', 'rat', 'elephant']

>>> spam[-1]

The integer value -1 refers to the last index in a list

'elephant' will be returned

300

dictionary methods that will return list-like values of the dictionary’s keys, values, or both keys and values

The keys(), values(), and items() Methods

400

>>> spam = ['cat', 'bat', 'rat', 'elephant']

>>> spam[10000]


Python will give you an IndexError error message if you use an index that exceeds the number of values in your list value.

400

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])

Ford will be returned

500

>>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]

>>> spam[0]

['cat', 'bat'] will be returned

500

-------- they can’t be sliced like lists

Because dictionaries are not ordered