How can I automate making dictionaries using python?

python dictionary
automate the boring stuff with python
automate the boring stuff with python, 2nd edition pdf
automate the boring stuff with python pdf download
python tutorial for automation
what to automate with python
automate the boring stuff with python list
python automation ideas

I am doing a beginners Python course and the aim is to make a bunch of dictionaries.

  1. Create three dictionaries: lloyd, alice, and tyler.

Give each dictionary the keys "name", "homework", "quizzes", and "tests".

Have the "name" key be the name of the student (that is, lloyd's name should be "Lloyd") and the other keys should be an empty list (We'll fill in these lists soon!)

I did this by doing the following:

def make_dict(list_of_names):
  for names in list_of_names:
    names = {
      "name": names,
      "homework" : [],
      "quizzes" : [],
      "tests" : []
  }

list_of_names = ["lloyd", 'alice', 'tyler']

make_dict(list_of_names)

Why does this not work? Should it work and is it just the Codeacademy development area that does not allow this to work? I realise I am being a little extra and that I could do this really straightforwardly and am purposely trying to be creative in how I do it.

In any case, what is the automated way to make a dictionary, based on lists of inputs?

You're creating a dictionary called names in each loop but not actually doing anything with it --

def make_dict(list_of_names):
    results = []
    for names in list_of_names:
        names = {
            "name": names,
            "homework" : [],
            "quizzes" : [],
            "tests" : []
        }
        results.append(names)
    return results

list_of_names = ["lloyd", 'alice', 'tyler']

my_dicts = make_dict(list_of_names)

This keeps track of the names dicts you have created, and then gives them to you at the end.

Dictionaries, Then, combining dictionaries with your knowledge of lists from the previous chapter, you'll learn how to create a data structure to model a tic-tac-toe board. What is the automated way to make a dictionary, based on lists of inputs? You can use a dictionary comprehension here. A generator can avoid the need for boilerplate list construction code. In this solution, we yield items for each name in a list. Calling list then exhausts the generator, and we can assign the list to the variable my_dicts.

What is the automated way to make a dictionary, based on lists of inputs?

You can use a dictionary comprehension here. A generator can avoid the need for boilerplate list construction code. In this solution, we yield items for each name in a list. Calling list then exhausts the generator, and we can assign the list to the variable my_dicts.

def make_dict(lst):
    for name in lst:
        d = {k: [] for k in ('homework', 'quizzes', 'tests')}
        d['name'] = name
        yield d

list_of_names = ['lloyd', 'alice', 'tyler']

my_dicts = list(make_dict(list_of_names))

Chapter 5 – Dictionaries and Structuring Data, Coding with Minecraft book cover thumbnail Cracking Codes with Python book cover thumbnail You create an initial dictionary and store it in birthdays ➀. The key must be immutable. This rule means that you can use strings, numbers, or tuples for the key. You can’t, however, use a list for a key. You have no restrictions on the values you provide. A value can be any Python object, so you can use a dictionary to access an employee record or other complex data.

You are creating three dictionaries; however, each one overwrites the previous one by assigning to the same variable names, and the last one is garbage collected because the only reference to it is a local variable that goes out of scope when make_dict returns.

You just need to return the created dict. For this exercise, it doesn't sound like you really need a loop.

def make_dict(name):
    return {
      "name": name,
      "homework" : [],
      "quizzes" : [],
      "tests" : []
    }

lloyd = make_dict("lloyd")
alice = make_dict("alice")
tyler = make_dict("tyler")

Three ways of creating dictionaries in Python, Hey Nick, cool article, I'm learning Python now in one of my classes. But under the “From a List of Tuples” in the last script, I think you meant to say  About dictionaries in Python. Use {} curly brackets to construct the dictionary, and [] square brackets to index it. Separate the key and value with colons : and with commas , between each pair. Keys must be quoted As with lists we can print out the dictionary by printing the reference to it.

Python : 6 Different ways to create Dictionaries – thispointer.com, We can use the python dictionary to keep this data, where the key will the string word and value is the frequency count. Now let's see different  Dictionaries are the fundamental data structure in Python, and a key tool in any Python programmer’s arsenal. They allow O(1) lookup speed, and have been heavily optimized for memory overhead and lookup speed efficiency. Today I”m going to show you three ways of constructing a Python dictionary, as well as some additional tips and tricks.

Python Dictionary, Check out what is a dictionary in Python, how to create, append, update, and delete elements. Also, learn to use comprehension with examples. The Automate the Boring Stuff with Python Programming online course on Udemy covers most of the content of the book. If you'd prefer a video format for learning to program, you can use this link to get a 70% discount. You will have lifetime access to the course content and can post questions to the course's forums.

Python Tutorial: Dictionaries, Tutorial on Dictionaries in Python: Operators and Methods of the Dictionary class. They shrink and grow without the necessity of making copies. Dictionaries can be contained in lists and vice The output of the previous script: {'blue': 'blau'  What You Can Do With Python. From web development to data science, machine learning, and more, Python’s real-world applications are limitless. Here are some projects that will assist you in finally putting your Python skills to good use. #1: Automate the Boring Stuff. This is a resource on “practical programming for total beginners.”

Comments
  • It is the development area. Ive had experiences with codecademy and their system only detects thats its right when its perfectly correct how they wrote it. It works perfectly in idle however. You might just want to skip.
  • Did one of the answers below help? If so, consider accepting (green tick on left), or ask for clarification.
  • Dang, beat me to it. To reiterate you need to return the variables you created. In this answer you are returning a list of dictionaries
  • Though this certainly works, you might want to rename the names = ... variable inside the loop to be something else, just to avoid confusion given that the OP is new to Python.
  • Thank you! In fact the very next exercise was actually to make the dictionaries into a list and so this is brilliant! Really appreciate it :)
  • Nice and clean for someone who really knows what they're doing but using fromkeys and yield in response to a question about a beginner python course might be a bit more opaque than necessary. I didn't downvote though, I like it
  • @MoxieBall, Sorry, dict.fromkeys won't work here as the same list will be referenced by each key. But a dict comprehension works too and, in my opinion, is readable. Generators, in my opinion, are underused, but conceptually straightforward.
  • Thanks for this. It seems like an advanced answer to my question and I will add this to my 'read when I am better' list - really appreciate the growth suggestion!
  • @noamcompsci, Sure, IMO it's always a good thing (when you learn about comprehensions and generators) to go back and see how you can approach a problem differently.
  • @jpp thanks for updating, I couldn't have told you at a glance that your original answer was wrong, definitely expanding my knowledge of how to leverage dictionaries