+ 2

How can we identify index of the items in the given list below?

List ={2:5,4:7,5:8}

15th May 2019, 6:16 AM
Hari Krishna Sahu
Hari Krishna Sahu - avatar
5 Respuestas
+ 8
As Anna said, it's a dictionary, and in this case we speak of "keys", "values" (and "items" for the general term). try this: dic ={5:2,2:7,6:8,5:4} print(dic.keys()) print(dic.values()) print(dic.items()) you will have: dict_keys([5, 2, 6]) dict_values([4, 7, 8]) dict_items([(5, 4), (2, 7), (6, 8)]) Note the effect of two entries for the key 5, the last one is kept.
15th May 2019, 8:41 AM
Cépagrave
Cépagrave - avatar
+ 7
Since Python 3.7, dictionaries are insertion-ordered. They will keep the order in which items were inserted. If I do this in IDLE with Python 3.5.2: d = {2: 5, 5: 8} d[4] = 7 # d[4] is inserted last print(d) the result is {2: 5, 4: 7, 5: 8} (items are in ascending order, not insertion-ordered). Output of the same code in Python 3.7.3: {2: 5, 5: 8, 4: 7} (items are insertion-ordered). You shouldn't rely on a dictionary's indices until you know for sure that Python version >= 3.7 is used (for earlier versions, there is OrderedDict).
15th May 2019, 7:02 AM
Anna
Anna - avatar
+ 4
That's a dictionary, not a list. Dictionaries don't have indices
15th May 2019, 6:30 AM
Anna
Anna - avatar
0
Index is the position of an element in the list, starting with 0. If you want to lookup an element and return it's position, use IndexOf(), but this is costly - O(n). If you can asume the list to be sorted, the complexity of IndexOf() will be reduced to O(log n).
15th May 2019, 6:27 AM
Daniel Adam
Daniel Adam - avatar