-3

I need to convert a PHP/JSON "object" (Python dict) into an array for filler words and show filler words with no count that will not be shown. How can I do this?

This is written as an object:

filler_word = {'um': 9, 'uh': 4, 'hmm': 0, 'mhm': 0, 'uh huh': 0}

This is how I want to have it into an array:

filler_word = ['um': 9, 'uh': 4, 'hmm': 0, 'mhm': 0, 'uh huh': 0]

This is how I want to show those filler words with 0 count that will not be shown:

filler_word = ['um': 9, 'uh': 4]
smci
  • 32,567
  • 20
  • 113
  • 146
Mashal Khan
  • 1
  • 1
  • 3

2 Answers2

1

You are bringing PHP thinking to your Python coding. The two are very different. What you have in the first line is a dictionary, which is the equivalent of a PHP array, and is used like a PHP array.

Python lists do not have keys. So, this is a list:

mylist = [ 9, 4, 0, 0 ]
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
-2

Use a dict comprehension:

{key: value for key,value in filler_word.items() if value > 0 }

{'um': 9, 'uh': 4}
smci
  • 32,567
  • 20
  • 113
  • 146