Discussion:
[Tutor] Create complex dictionary :p:
Thomas C. Hicks
2015-10-22 21:50:14 UTC
Permalink
complex = {name ="value",surname="po",age=poi)
What is the most pythonic way to build a dictionary of dictionary?thanks for any help!
This doesn't look too complex so I am probably missing something.

The normal dictionary construction would look something like this:

mydict = dict('name'='value', 'surname'='po','age'='poi')

Then you can access any given item in mydict with the get method:

mydict.get('name')

SDG,

tom
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alex Kleider
2015-10-22 21:59:05 UTC
Permalink
Post by Thomas C. Hicks
complex = {name ="value",surname="po",age=poi)
What is the most pythonic way to build a dictionary of
dictionary?thanks for any help!
This doesn't look too complex so I am probably missing something.
mydict = dict('name'='value', 'surname'='po','age'='poi')
mydict.get('name')
SDG,
tom
***@x301:~$ python3
Python 3.4.3 (default, Jul 28 2015, 18:24:59)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
Post by Thomas C. Hicks
Post by Thomas C. Hicks
mydict = dict('name'='value', 'surname'='po','age'='poi')
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
my understanding is that you could have done it in either of the
following two ways:
1: mydict = dict(name='value', surname='po',age='poi')
2: mydict = {'name': 'value', 'surname': 'po','age': 'poi'}

Also, accessing any given item might be done as follows:
mydict['name']
rather than calling the get method, n'est pas?


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Thomas C. Hicks
2015-10-22 23:08:01 UTC
Permalink
Post by Thomas C. Hicks
mydict = dict('name'='value', 'surname'='po','age'='poi')
Oops, you are correct! Don't want to put the key names in quotes, I
mistyped my experiment.

SDG,

tom
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Cameron Simpson
2015-10-22 21:54:58 UTC
Permalink
complex = {name ="value",surname="po",age=poi)
Well, in Python you'd write the above like this:

complex = {'name': "value", 'surname': "po", 'age': poi}
What is the most pythonic way to build a dictionary of dictionary?
Your description is a bit vague, but it sounds like a dictionary or
dictionaries is a reasonable way to do it. Example:

records = {}
complex = {'name': "value", 'surname': "po", 'age': poi}
records['key1'] = complex
complex = {'name': "value2", 'surname': "xy", 'age': poi2}
records['key2'] = complex

Nothing wrong with that if it fits what you actually need to do.

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-10-22 23:00:14 UTC
Permalink
complex = {name ="value",surname="po",age=poi)
That's invalid syntax (the braces don't match) and it seems to use names
from elsewhere.

If you mean a dictionary like this::

wibble = {'name': "value", 'surname': "po", 'age': 42}

then that is a simple dictionary. So I don't know what you mean by a
“complex” dictionary.
What is the most pythonic way to build a dictionary of
dictionary?
You can assign any value to any key in a dictionary. Dictionaries are
also values, so a dictionary can be assigned to a key just like any
other value can be assigned to a key.

wobble = {
'name': "Lorem Ipsum",
'age': 42,
'ratings': {
'badminton': 17.48,
'celery': None,
'fussball': 0.14,
},
'address': "175 West Arglbargle, Lower Snootbatten",
}

That has values that are themselves containers, so I suppose it counts
as a “complex” dictionary.
--
\ “I went to the museum where they had all the heads and arms |
`\ from the statues that are in all the other museums.” —Steven |
_o__) Wright |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
http
Loading...