473,241 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 8351
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
90
by: Jhon smith | last post by:
Hi all,Just wondering are there any problems with learning c from older books,as I have picked up some from 1988,1994,1997,1998. By using books of this age(Im on a tight budget)am I going to...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
3
by: Faisal Alquaddoomi | last post by:
Hello, I'm having a bit of trouble isolating my scripts from each other in my embedded Python interpreter, so that their global namespaces don't get all entangled. I've had some luck with...
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.