Accessing specific key in list of dictionaries in Python

Question:

I’m being asked to write a function which will accept a list of dictionary objects as input, and will return a list of values pertaining to a specific key in the dictionaries. An example of the function call:

titles = getKeys( [book1, book2, book3] )

enter image description here

I made 3 dictionaries and then defined the function and passed a list of the dictionaries as arguments. I know that I need a for loop to parse through the list but don’t know how exactly that is done.

  book1 = {
        "Title": "ShowYourWork",
        "Price": 2.99,
        "Edition": "5th",
        "in_stock": False
        }
    book2 = {
        "Title": "HowToNotDieALone",
        "Price": 10.00,
        "Edition": "2nd",
        "in_stock": True
        }
    book3 = {
        "Title": "TheSecondBrain",
        "Price": 9.99,
        "Edition": "8th",
        "in_stock": False
        }

    books = [book1, book2, book3]
        
    def getKeys(books):
        for book in books:
            print(f"{book['Price']}")  
Asked By: Ahmed ElAbbas

||

Answers:

First if you need to return a list, you need to use return statement.

Second, if the directionary is suppose to have the key "Title" you can modify the last function and use a list comprehension

def getKeys(books):
   return [book['Title']  for book in books]
Answered By: Lucas M. Uriarte

Based on the provided code, it seem you have not actually called the getKeys method, you’ve only defined it. Just because your list of dictionaries has the same name as the parameter of the getKeys method does not mean getKeys was ever called.

book1 = {
        "Title": "ShowYourWork",
        "Price": 2.99,
        "Edition": "5th",
        "in_stock": False
        }
book2 = {
        "Title": "HowToNotDieALone",
        "Price": 10.00,
        "Edition": "2nd",
        "in_stock": True
        }
book3 = {
        "Title": "TheSecondBrain",
        "Price": 9.99,
        "Edition": "8th",
        "in_stock": False
        }

books = [book1, book2, book3]
        
def getKeys(books):
    for book in books:
        print(f"{book['Price']}")

# this actually passes the list variable books into the getKeys method
# and executes the code in the method body
getKeys(books)

In Python, def does not execute the method, it only defines what the method is and does. You still have to call it yourself. You can read about defining and calling functions in Python here.

Answered By: noahG3
book1 = {
        "Title": "ShowYourWork",
        "Price": 2.99,
        "Edition": "5th",
        "in_stock": False
        }
book2 = {
        "Title": "HowToNotDieALone",
        "Price": 10.00,
        "Edition": "2nd",
        "in_stock": True
        }
book3 = {
        "Title": "TheSecondBrain",
        "Price": 9.99,
        "Edition": "8th",
        "in_stock": False
        }

books = [book1, book2, book3]
        
def getKeys(books):
    return [book['Title']  for book in books]
    
titles = getKeys(books)
print(titles)
Answered By: Ahmed ElAbbas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.