Home > OS >  Why dictionary comprehention gives only 3 elements
Why dictionary comprehention gives only 3 elements

Time:07-28

Why does this code only give three elements as output?

num_list1 = [6,34,-55,65,2,0,-6,77,66,55,44,33]
new_dict = {'positive' if key > 0 else 'negative' if key < 0 else 'zero': key for key in num_list1}
print(new_dict)

Output:

{'positive': 33, 'negative': -6, 'zero': 0}

But another record returns all elements:

num_list1 = [6,34,-55,65,2,0,-6,77,66,55,44,33]
new_dict = {key: 'positive' if key > 0 else 'negative' if key < 0 else 'zero' for key in num_list1}
print(new_dict)

Output:

{6: 'positive', 34: 'positive', -55: 'negative', 65: 'positive', 2: 'positive', 0: 'zero', -6: 'negative', 77: 'positive', 66: 'positive', 55: 'positive', 44: 'positive', 33: 'positive'}

Thanks

CodePudding user response:

another record returns all elements

Does it, actually, though?

>>> num_list1 = [6,34,-55,65,2,0,-6,77,66,55,44,33,2]
>>> print(len(num_list1))
13
>>> new_dict = {key: 'positive' if key > 0 else 'negative' if key < 0 else 'zero' for key in num_list1}
>>> print(len(new_dict))
12

Notice I added a 2 at the end. Dictionaries cannot have duplicate keys.

If you wanted to count the number of positive, negative, and zero elements, then use a plain for-loop and if-else statements.

from collections import defaultdict
data = defaultdict(int)
for x in num_list1:
   if x < 0:
     data["negative"]  = 1
   elif x > 0:
     data["positive"]  = 1
   else:
     data["zero"]  = 1
print(data.items())  # dict_items([('positive', 10), ('negative', 2), ('zero', 1)])

Or, if you do want the values, you can use list-comprehension in a dictionary literal with unique keys.

data = {
  "positives": [(i, x) for i, x in enumerate(num_list1) if x > 0], 
  "negatives": [(i, x) for i, x in enumerate(num_list1) if x < 0], 
  "zeros": [(i, x) for i, x in enumerate(num_list1) if x == 0]
}
# {'positives': [(0, 6), (1, 34), (3, 65), (4, 2), (7, 77), (8, 66), (9, 55), (10, 44), (11, 33), (12, 2)], 'negatives': [(2, -55), (6, -6)], 'zeros': [(5, 0)]}

CodePudding user response:

This is because dictionaries can't have duplicate keys. Every time the previous value is overwritten by the new value, so it only saves the last positive, negative and zero values.

If you want to count positive, negative and zeroes, use this:

>>> count = {}
>>> for x in num_list1:
...     count.update({'positive' if x > 0 else 'negative' if x < 0 else 'zero': count.get('positive' if x > 0 else 'negative' if x < 0 else 'zero', 0)   1})
...
>>> count
{'positive': 10, 'negative': 2, 'zero': 1}

Or, with collections.defaultdict:

>>> from collections import defaultdict
>>> count = defaultdict(int)
>>> for x in num_list1:
...     count['positive' if x > 0 else 'negative' if x < 0 else 'zero']  = 1
...
>>> dict(count)
{'positive': 10, 'negative': 2, 'zero': 1}

If you want to get all the positive, negative, and zero numbers:

>>> from collections import defaultdict
>>> nums = defaultdict(list)
>>> for x in num_list1:
...     nums['positive' if x > 0 else 'negative' if x < 0 else 'zero'].append(x)
...
>>> dict(nums)
{'positive': [6, 34, 65, 2, 77, 66, 55, 44, 33, 2], 'negative': [-55, -6], 'zero': [0]}

CodePudding user response:

In the second one, you just inversed key and value in your dictionnary value : {positive, zero, negative} instead of {positive, zero, negative} : value

Just change new_dict = {key: 'positive' if key > 0 else 'negative' if key < 0 else 'zero' for key in num_list1} to new_dict = {'positive' if key > 0 else 'negative' if key < 0 else 'zero' : key for key in num_list1}

First example show last positive, last zero and last negative Second says "6 is positive, 34 is positive..."

  • Related