Home > database >  Printing Python Key elements by Value in List of Dictionaries
Printing Python Key elements by Value in List of Dictionaries

Time:08-09

I have a question that I think is fairly simple but I can't figure out the syntax to.

I have a list of dictionaries which looks like this:

[{'word': 'the', 'count': 19}, 
 {'word': 'and', 'count': 14}, 
 {'word': 'he', 'count': 7}, 
 {'word': 'left', 'count': 1}, 
 {'word': 'dead', 'count': 1}, 
 {'word': 'its', 'count': 1}, 
 {'word': 'head', 'count': 1}, 
 {'word': 'galumphing', 'count': 1}, 
 {'word': 'arm', 'count': 1}, 
 {'word': 'beamish', 'count': 1}, 
 {'word': 'boy', 'count': 1}, 
 {'word': 'o', 'count': 1}]

I simply want a for loop which accesses the count variable and prints every dictionary where count = 1. The output I'm looking for:

[{'word': 'left', 'count': 1}, 
 {'word': 'dead', 'count': 1}, 
 {'word': 'its', 'count': 1}, 
 {'word': 'head', 'count': 1}, 
 {'word': 'galumphing', 'count': 1}, 
 {'word': 'arm', 'count': 1}, 
 {'word': 'beamish', 'count': 1}, 
 {'word': 'boy', 'count': 1}, 
 {'word': 'o', 'count': 1}]

What would be the easiest way to achieve something like this! Thanks.

CodePudding user response:

You can use a list comprehension. If you have:

lst = [{'word': 'the', 'count': 19},
       {'word': 'and', 'count': 14},
       {'word': 'he', 'count': 7},
       {'word': 'left', 'count': 1},
       {'word': 'dead', 'count': 1},
       {'word': 'its', 'count': 1},
       {'word': 'head', 'count': 1},
       {'word': 'galumphing', 'count': 1},
       {'word': 'arm', 'count': 1},
       {'word': 'beamish', 'count': 1},
       {'word': 'boy', 'count': 1},
       {'word': 'o', 'count': 1}]

You can do:

lst2 = [d for d in lst if d['count'] == 1]

This sets lst2 to:

[{'word': 'left', 'count': 1},
 {'word': 'dead', 'count': 1},
 {'word': 'its', 'count': 1},
 {'word': 'head', 'count': 1},
 {'word': 'galumphing', 'count': 1},
 {'word': 'arm', 'count': 1},
 {'word': 'beamish', 'count': 1},
 {'word': 'boy', 'count': 1},
 {'word': 'o', 'count': 1}]

CodePudding user response:

I worked out a solution to this problem myself shortly after posting the question. The easiest way to do it is this:

for x in array:
    if x['count'] == 1:
    print(x)

This prints the values where the specific count equals 1!

CodePudding user response:

You can Just Make a List Variable And if the dicts count key has value 1, append the dict to the List Variable!

As Shown Here!

Code:

result = []
list_of_dict = [{'word': 'the', 'count': 19},
 {'word': 'and', 'count': 14},
 {'word': 'he', 'count': 7},
 {'word': 'left', 'count': 1},
 {'word': 'dead', 'count': 1},
 {'word': 'its', 'count': 1},
 {'word': 'head', 'count': 1},
 {'word': 'galumphing', 'count': 1},
 {'word': 'arm', 'count': 1},
 {'word': 'beamish', 'count': 1},
 {'word': 'boy', 'count': 1},
 {'word': 'o', 'count': 1}]
for i in list_of_dict:
    if i["count"] == 1:
      result.append(i)
print(result)

Output:

    [{'word': 'left', 'count': 1}, 
     {'word': 'dead', 'count': 1}, 
     {'word': 'its', 'count': 1}, 
     {'word': 'head', 'count': 1}, 
     {'word': 'galumphing', 'count': 1}, 
     {'word': 'arm', 'count': 1}, 
     {'word': 'beamish', 'count': 1}, 
     {'word': 'boy', 'count': 1}, 
     {'word': 'o', 'count': 1}]

Its The Simplest Way To Do It

  • Related