By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

Learning about dictionaries

P: n/a
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
dict(1='one',2='two') SyntaxError: keyword can't be an expression

As this did not work, I tried dict(1=one,2=two) SyntaxError: keyword can't be an expression

and dict('1'='one','2'='two') SyntaxError: keyword can't be an expression

as well as dict('1'=one,'2'=two) SyntaxError: keyword can't be an expression

Out of curiosity, I tried dict(one=1,two=2) {'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 dict(zip((1,2),('one','two'))) {1: 'one', 2: 'two'}
or dict(((1,'one'),(2,'two')))

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

Sincerely

Thomas Philips
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Thomas Philips wrote:
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
dict(1='one',2='two')
SyntaxError: keyword can't be an expression

As this did not work, I tried
dict(1=one,2=two)
SyntaxError: keyword can't be an expression

and
dict('1'='one','2'='two')
SyntaxError: keyword can't be an expression

as well as
dict('1'=one,'2'=two)
SyntaxError: keyword can't be an expression

Out of curiosity, I tried
dict(one=1,two=2)
{'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
dict(zip((1,2),('one','two')))
{1: 'one', 2: 'two'}
or
dict(((1,'one'),(2,'two')))
{1: 'one', 2: 'two'}

Sincerely

Thomas Philips

d={1:'one',2:'two'}
d {1: 'one', 2: 'two'} dd=dict(d)
dd

{1: 'one', 2: 'two'}
Jul 18 '05 #2

P: n/a
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
dict(1='one',2='two') 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 dict(one=1,two=2) {'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
<object>). This means that you don't have to specify the parameters in
advance like this:
def myfunc(a, b): .... pass

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

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

{'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):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #3

P: n/a
Thomas Philips wrote:
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
dict(1='one',2='two')


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>


Jul 18 '05 #4

P: n/a
Thomas Philips wrote:
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
dict(1='one',2='two')
SyntaxError: keyword can't be an expression

As this did not work, I tried
dict(1=one,2=two)
SyntaxError: keyword can't be an expression

and
dict('1'='one','2'='two')
SyntaxError: keyword can't be an expression

as well as
dict('1'=one,'2'=two)
SyntaxError: keyword can't be an expression

Out of curiosity, I tried
dict(one=1,two=2)
{'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
dict(zip((1,2),('one','two')))
{1: 'one', 2: 'two'}
or
dict(((1,'one'),(2,'two')))
{1: 'one', 2: 'two'}

Sincerely

Thomas Philips

Thomas,
Does this help:
print(dict.__doc__) 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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.