Reputation: 143
For example, I have this:
mydict = {0: ['ID1', 46, 10, 2.0], 1: ['ID2', 23, 9, 1.85]}
I would like to know how to make comparisons of the forth value for the 2 lists through accessing the keys of the dictionary.
Below codes is what I have tried but I cannot get the forth item (float value) in the list to compare:
for val in mydict.keys():
for item in mydict[val]:
print(item)
Upvotes: 0
Views: 41
Reputation: 86
For accessing the list you don't need an internal loop, just get it right by index. The code may look like:
mydict = {0: ['ID1', 46, 10, 2.0], 1: ['ID2', 23, 9, 1.85]}
prev = -1
isChecked = False
for key in mydict.keys():
myList = mydict[key]
if(isChecked):
print(prev == myList[3])
else:
isChecked = True
prev = myList[3]
Upvotes: 1
Reputation: 23825
Try
mydict = {0: ['ID1', 46, 10, 2.0], 1: ['ID2', 23, 9, 1.85]}
# just show the entries for keys 1 & 2
print(f'{mydict[0][3]} and {mydict[1][3]}')
# or - loop over the keys:
for k in mydict.keys():
print(f'{mydict[k][3]}')
Upvotes: 0
Reputation: 689
Since python is a 0-based language, lst[3]
is the fourth number.
So try:
mydict[0][3] == mydict[1][3]
and this will be true if and only if the fourth elements are the same.
Upvotes: 0