471,073 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Learning about dictionaries

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
4 8243
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
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
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
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.

Similar topics

reply views Thread by Till Plewe | last post: by
8 posts views Thread by Frohnhofer, James | last post: by
3 posts views Thread by Shivram U | last post: by
90 posts views Thread by Jhon smith | last post: by
210 posts views Thread by Christoph Zwerschke | last post: by
2 posts views Thread by David Pratt | last post: by
3 posts views Thread by Faisal Alquaddoomi | last post: by
8 posts views Thread by placid | last post: by
14 posts views Thread by cnb | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.