trying to understand dictionaries

K

khemeia

Hi.
As the subject says, I'm a newbie trying to learn python and now
dictionaries. I can create a dict, understand it and use it for simple
tasks. But the thing is that I don't really get the point on how to
use these in real life programing.

For example I tryed to create a very simple phonebook

code:

d = {'fname': [], 'ename': []}
name1 = 'ricky'
name2 = 'martin'
d['fname'].append(name1)
d['ename'].append(name2)

name1 = 'britney'
name2 = 'spears'
d['fname'].append(name1)
d['ename'].append(name2)


This gives me:
{'ename': ['martin', 'spears'], 'fname': ['ricky', 'britney']}

I wonder if this is a correct usage and thinking about dictioaries in
python.
Everything in my example is based on a serval lists in a dictionary,
and person 1 is == ename[0] & fname[0]
and person 2 is == ename[1] & fname[1], it's based on position and
indexing.

Is this correct if no, how would you do it?
if yes, how can I print the result out in a nice way? I need a for-
loop that prints:
all [0] in all lists
all [0] in all lists and so on.


thanks
/jonas
 
J

Jean-Michel Pichavant

Basically, dictionaries are a bunch of couples of key and value where
key is an immutable object. In fact you'll find a more accurate
definition in any python book or online tutorial.
The thing is that keys are usually used as an easy and quick way the
search and index items.
You can see dict as a list that can use strings instead of integers to
index its values.

phonebook = {
'ricky' : ('ricky', 'martin', 'male'),
'britney' : ('britney', 'spears', 'female'),
'myBestBuddy' : ('barack', 'obama', 'male'),
42 : ('bob', 'bib', 'male'), # you can use 42 as index, like strings
integers are not mutable
True: ('', '', ''), # meaningless example just to show that any
immutable object will fit as index
}

fname, ename, gender = phonebook['myBestBuddy']
fname, ename, gender = phonebook[42]

Keys are unique among the dictionary.

Jean-Michel
 
A

Alan G Isaac

d = {'fname': [], 'ename': []}
name1 = 'ricky'
name2 = 'martin'
d['fname'].append(name1)
d['ename'].append(name2)

name1 = 'britney'
name2 = 'spears'
d['fname'].append(name1)
d['ename'].append(name2)


This gives me:
{'ename': ['martin', 'spears'], 'fname': ['ricky', 'britney']}


Trying to stick close to your example,
reverse the use of lists and dicts.

worst_musicians = list()
entry = dict(fname='ricky',lname='martin')
worst_musicians.append(entry)
entry = dict(fname='britney',lname='spears')
worst_musicians.append(entry)

for musician in worst_musicians:
print "%(fname)s %(lname)s"%musician

hth,
Alan Isaac
 
A

Albert Hopkins

Hi.
As the subject says, I'm a newbie trying to learn python and now
dictionaries. I can create a dict, understand it and use it for simple
tasks. But the thing is that I don't really get the point on how to
use these in real life programing.

For example I tryed to create a very simple phonebook

code:

d = {'fname': [], 'ename': []}
name1 = 'ricky'
name2 = 'martin'
d['fname'].append(name1)
d['ename'].append(name2)

name1 = 'britney'
name2 = 'spears'
d['fname'].append(name1)
d['ename'].append(name2)


This gives me:
{'ename': ['martin', 'spears'], 'fname': ['ricky', 'britney']}

I wonder if this is a correct usage and thinking about dictioaries in
python.
Everything in my example is based on a serval lists in a dictionary,
and person 1 is == ename[0] & fname[0]
and person 2 is == ename[1] & fname[1], it's based on position and
indexing.

Is this correct if no, how would you do it?
if yes, how can I print the result out in a nice way? I need a for-
loop that prints:
all [0] in all lists
all [0] in all lists and so on.

That's probably not the data model I'd use for a phone book. The
following isn't either, but is better:

d = dict()
d[('martin', 'ricky')] = '555-1212'
d[('spears', 'britney')] = '555-1213'

for last, first in d:
phone_number = d[(last, first)]
print ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top