Learning about dictionaries

T

Thomas Philips

I'm teaching myself python and in the course of playing around with
dictionaries, I tried to create the following trivial dictionary

{1:'one', 2:'two'}

So I enteredSyntaxError: keyword can't be an expression

As this did not work, I triedSyntaxError: keyword can't be an expression

andSyntaxError: keyword can't be an expression

as well asSyntaxError: keyword can't be an expression

Out of curiosity, I tried{'two': 2, 'one': 1}

Why does this last attempt work, and more importantly, why did my four
earlier attempts fail? I might add that I have no trouble getting what
I want with{1: 'one', 2: 'two'}
or{1: 'one', 2: 'two'}

Sincerely

Thomas Philips
 
F

Fredrik Lundh

Thomas said:
I'm teaching myself python and in the course of playing around with
dictionaries, I tried to create the following trivial dictionary

{1:'one', 2:'two'}

that's a dictionary, right.

(to be precise, it's a "dictionary display" which is how you enter
"dictionary values" in Python source code)
So I entered

that's not a dictionary, that's a function call.

you're trying to call a function called "dict" with two keyword arguments,
but your keywords doesn't match Python's identifier syntax.

more here:

http://docs.python.org/ref/calls.html
http://docs.python.org/ref/dict.html

</F>
 
W

wes weston

Thomas said:
I'm teaching myself python and in the course of playing around with
dictionaries, I tried to create the following trivial dictionary

{1:'one', 2:'two'}

So I entered


SyntaxError: keyword can't be an expression

As this did not work, I tried


SyntaxError: keyword can't be an expression

and


SyntaxError: keyword can't be an expression

as well as


SyntaxError: keyword can't be an expression

Out of curiosity, I tried


{'two': 2, 'one': 1}

Why does this last attempt work, and more importantly, why did my four
earlier attempts fail? I might add that I have no trouble getting what
I want with


{1: 'one', 2: 'two'}
or


{1: 'one', 2: 'two'}

Sincerely

Thomas Philips
{1: 'one', 2: 'two'}
 
A

Andrei

Thomas Philips wrote on Friday 16 April 2004 18:50:
I'm teaching myself python and in the course of playing around with
dictionaries, I tried to create the following trivial dictionary

{1:'one', 2:'two'}

So I entered
SyntaxError: keyword can't be an expression

Try doing 1 = 'one' in the interactive interpreter. Does it work? Nope. Did
you expect it to work? (Hopefully not :).) So obviously it doesn't work
inside a function call neither.
Out of curiosity, I tried
{'two': 2, 'one': 1}

Bingo :). Python has a special construct for functions which accept any
numbers of keyword arguments (these are arguments which have the form of
<name>=<object>, as opposed to non-keyword arguments which are just
.... pass

Instead, you allow any number of parameters to be passed to the function
using '**'
.... print kwds
.... # kwds is a dictionary containing all parametername-value pairs
....{'a': '5', 'c': True, 'b': 6}

You can modify myfunc now very easily to behave like dict() in this
particular case:
.... return kwds
....{'a': '5', 'c': True, 'b': 6}

The method Wes specified for creating dictionaries is more useful and usable
IMO than dict() with keyword parameters.

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 
W

wes weston

Thomas said:
I'm teaching myself python and in the course of playing around with
dictionaries, I tried to create the following trivial dictionary

{1:'one', 2:'two'}

So I entered


SyntaxError: keyword can't be an expression

As this did not work, I tried


SyntaxError: keyword can't be an expression

and


SyntaxError: keyword can't be an expression

as well as


SyntaxError: keyword can't be an expression

Out of curiosity, I tried


{'two': 2, 'one': 1}

Why does this last attempt work, and more importantly, why did my four
earlier attempts fail? I might add that I have no trouble getting what
I want with


{1: 'one', 2: 'two'}
or


{1: 'one', 2: 'two'}

Sincerely

Thomas Philips


Thomas,
Does this help:
dict() -> new empty dictionary.
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs.
dict(seq) -> new dictionary initialized as if via:
d = {}
for k, v in seq:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
wes
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top