Home arrow Python arrow Page 5 - Python 101 (part 4): Feeding The Snake

Looking Up The Dictionary - Python

Luke Skywalker has the Force. Python programmers havedictionaries and tuples. What's the connection? Read on to find out...

  1. Python 101 (part 4): Feeding The Snake
  2. Running The Lights
  3. Strange Food
  4. Unbreakable
  5. Looking Up The Dictionary
  6. Of Keys And Locks
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 8
June 25, 2001

print this article


Unlike tuples, dictionaries are a "mutable" object type, which means that the elements contained within them can be changed at will.

As you've already seen, adding a new element to a dictionary is as simple as assigning a value to a key.

>>> characters = {"new hope":"Luke", "teacher":"Yoda", "bad guy":"Darth"} >>> characters["princess"] = "Leia" >>> characters["worse guy"] = "The Emperor" >>> characters {'princess': 'Leia', 'teacher': 'Yoda', 'new hope': 'Luke', 'bad guy': 'Darth', 'worse guy': 'The Emperor'} >>>
You can also use this technique to update existing dictionary elements with new values,

>>> characters["teacher"] = "Obi-Wan" >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth', 'worse guy': 'The Emperor'} >>>
or use the del() method to remove individual key-value pairs from the dictionary.

>>> del (characters["worse guy"]) >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>>
A built-in update() method makes it possible to copy elements from one dictionary to another.

>>> charactersCopy = {} >>> charactersCopy.update(characters) >>> charactersCopy {'teacher': 'Obi-Wan', 'princess': 'Leia', 'new hope': 'Luke', 'bad guy': 'Darth'} >>>
while a clear() method allows you to empty the container of all elements,

>>> charactersCopy.clear() >>> charactersCopy {} >>>
The built-in len() function can be used to calculate the number of key-value pairs in a dictionary.

>>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> len (characters) 4 >>>
It should be noted at this point that dictionaries do not support concatenation or repetition, and, since they don't use numerical indices, it's not possible to extract slices of a dictionary either. In fact, attempting this will cause Python to barf all over your screen,

>>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> opposites {'day': 'night', 'white': 'black', 'yes': 'no'} >>> newDict = characters + opposites Traceback (innermost last): File "", line 1, in ? TypeError: bad operand type(s) for + >>>
as will the deadly sin of trying to access a key which doesn't exist.

>>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters["robot"] Traceback (innermost last): File "", line 1, in ? KeyError: robot >>>
As a matter of fact, you can use the has_key() built-in method to check whether or not a particular key exists, thereby making it easier to avoid errors like the one above.

>>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters.has_key("teacher") 1 >>> characters.has_key("princess") 1 >>> characters.has_key("robot") 0 >>>

>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: