Python Reference:  Blender 3D Game Engine



 



Dictionary

Creating a Dictionary

A Dictionary is a data structure.  The data is stored using a Key (ie. name) and  a Value (ie. the data).  Data can be retrieved from the dictionary with the Key.

The Key and Value pair are separated by a colon (:).

The dictionary is enclosed with brackets and the data (Key : Value pairs) is separated by commas. 

A dictionary can hold numbers, objects, strings, lists, etc

Example:


# create a dictionary of phone numbers
phonebook  = { "Fred"     : "555-1111", 
"Alison"   : "555-2222",
"George" : "555-3333",
"Rachel"  : "555-4444" }

Retrieving data from a dictionary:

Data is retrieved by using the key (ie name) of the data.

Example:


# create a dictionary of phone numbers
phonebook  = { "Fred"     : "555-1111", 
"Alison"   : "555-2222",
"George" : "555-3333",
"Rachel"  : "555-4444" }


# get Rachel's phone number
number = phonebook["Rachel"]

# print Rachel's phone number
print(number)

########   it will print

555-4444

########
   
Adding data to a dictionary:

Data is added to the dictionary using a new key (ie name).

Example:


# create a dictionary of phone numbers
phonebook  = { "Fred"     : "555-1111", 
"Alison"   : "555-2222",
"George" : "555-3333",
"Rachel"  : "555-4444" }

# add Daniella to phonebook

phonebook["Daniella"] = "555-5555"

# print phonebook
print(phonebook)

########   it will print

{ "Daniella" : "555-5555", "Fred" : "555-1111",  "Alison" : "555-2222", "George" : "555-3333", "Rachel" : "555-4444" }

########

Changing data in a dictionary:

Data is changed by using the key (ie name).

Example:


# create a dictionary of phone numbers
phonebook  = { "Fred"     : "555-1111", 
"Alison"   : "555-2222",
"George" : "555-3333",
"Rachel"  : "555-4444" }

# change Fred's phone number
phonebook["Fred"] = "555-1234"

# print phonebook
print(phonebook)

########   it will print

{ "Fred" : "555-1234",  "Alison" : "555-2222", "George" : "555-3333", "Rachel" : "555-4444" }

########

Removing data from a dictionary:

Data is removed from the dictionary by using the del command to delete the key (ie name) of the data.

Example:


# create a dictionary of phone numbers
phonebook  = { "Fred"     : "555-1111", 
"Alison"   : "555-2222",
"George" : "555-3333",
"Rachel"  : "555-4444" }

# remove Fred from phonebook
del(phonebook["Fred"])

# print phonebook
print(phonebook)

########   it will print

{  "Alison" : "555-2222", "George" : "555-3333", "Rachel" : "555-4444" }

########
  





The Blender 3D and Blender 3D game engine tutorials were created as a series of step by step instructions to help you create computer games. Casual video games, First Person Shooter, role playing games, racing simulations and more. Use the Blender 3D tutorials to model and program your own three-dimensional world and then play on your computer.