0

About the dictionaries I read that they return keys in the insertion order but in the following code when I print dict it should return {1.0:4, 5.0:1, 3.0:3} but result is different. It returns the values in ascending order of keys. I want exactly the same order because I have add elements in the corresponding list b. If order changes I can not deal b list.

`a = [1.0, 1.0, 1.0, 1.0, 5.0, 3.0,3.0,3.0]
b=[2, 2, 2, 2, 1, 5, 5, 5]
dict ={}
for val in a:
    if val not in dict:
        dict[val] = 1
    else:
        dict[val] += 1
print(dict)`
user10163680
  • 253
  • 2
  • 11
  • 3
    Python dict-objects are only guaranteed to be ordered in versions >=3.7. In version 3.6, in CPython, they were ordered, but that was considered an implementation detail. – juanpa.arrivillaga Sep 08 '18 at 03:47
  • Why not make a dict mapping `a => {b, val}` or `(a,b) => val`, or a list of dicts `{a, b, val}`? – BallpointBen Sep 08 '18 at 05:35
  • can you please explain by short code how to do that mapping – user10163680 Sep 08 '18 at 05:50
  • I make dictionary for a single list and count values at a specific number e.g at 1.0 there are 4 values . Then I again apply a for loop and pop the elements for b in sub list . Although I have obtained the desired result. Now looking for alternate easy way – user10163680 Sep 08 '18 at 05:52
  • You've received ans answer to the question as you asked it. If you need to know how to do what you're trying to do, ask a different question about that. – ivan_pozdeev Sep 09 '18 at 21:16

0 Answers0