nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']
print nameList([1][-1])
k will be returned by print statement.
>>> 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
Slicing
What we call the operation which is used to extract particular range from a sequence.
>>> spam = {'color': 'red', 'age': 42}
>>> for v in spam.values():
print(v)
It will return
red
42
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-1]
The integer value -1 refers to the last index in a list
'elephant' will be returned
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
>>> 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.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Ford will be returned
>>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
>>> spam[0]
['cat', 'bat'] will be returned
-------- they can’t be sliced like lists
Because dictionaries are not ordered