Home > Enterprise >  how do i automagicly create a dict in my editor with keys 0-100
how do i automagicly create a dict in my editor with keys 0-100

Time:08-17

I want to create a dictionary with keys 0-1000. I want to use this dict as a "database" to store hardcoded data

Question

Doing this by hand costs a fair bit of time. Is there a way I can do this automatically?

Example

usernames: dict = {
    0: "",
    1: "",
    2: "",
    3: "",
    4: "",
    5: "",
    6: "",
    7: "",
    8: "",
    9: "",
    10: "",
    # and so on ...
    1000: ""
}

CodePudding user response:

Consider using comprehension

usernames: dict = {idx: "" for idx in range(1001)}

CodePudding user response:

Use the classmethod factory fromkeys:

dict.fromkeys(range(1001), "")

And perhaps consider using a list. This is more memory efficient than using a dict with consecutive integer keys.

[""] * 1001

CodePudding user response:

Start by creating a dictionary:

dictionary = {}

Now you can use a for loop to create the keys:

for num in range(1001):
    dictionary[num] = ""

CodePudding user response:

Try this.

usernames = {}
for i in range(0, 100):
    usernames[i] = ""

CodePudding user response:

This looks like the kind of thing you should do programmatically rather than manually editing a file. For sequential numeric keys, consider using a list instead of a dict.

If you really need a dict, then use a dict comprehension as Demitri shows or defaultdict which allows you to provide a default value for any key that isn't explicitly set in the dict.

If you have some data other than just empty strings, then consider reading the data from an external file rather than putting it in code. Generally, data should be stored separately from code anyway.

CodePudding user response:

Assuming your use-case:

want to use this dict as a "database" to store hardcoded data

The database approach

Most databases don't reserve space upfront and create something like dummy-records. Nor do they create and reserve keys in advance.

What a typical database and the setup for a collection or table of users would do:

  1. create the schema or table user with columns or fields id for the key or sequential number, name more for the attributes.
  2. create the sequence or auto-numbering to generate a new id value for each newly added or inserted record

The data structure approach in Python

Analogously you would create either a dict with some functions to manage:

user_store = {}  # empty dict, no schema, no elements yet

current_id = None  # the sequence to increment for each new record added

def nextId():
    if not current_id:
       current_id = 0  # the initial first value of the sequence
    else:
       current_id  = 1  # increment each time

    return current_id

def add_user(name):
    id = nextId()  # generate unique id from sequence
    user_store[id] = name  # add new key with value 
    return id  # return the generated id to reference the user record later 

def remove_user_by_id(id):
    del user_store[id]  # frees space

def update_user(id, new_name):
    user_store[id] = new_name  # update the name


def count():
    # there may be records and ids deleted, so the count is not related to current_id
    return len(user_store)

See also:

If the (primary) key or id is a sequential number starting with 0 you can also use a list instead a dict.

  • Related