python dictionary
By: Ashley J
An item in a dictionary is a key/value pair. You can think of a dictionary as an associative array (known in some other languages as an “unordered map,†“hash table,†or “hashâ€). Keys in a dictionary may be of different types, but they must be hashable. Values in a dictionary are arbitrary objects and may be of any type.
To denote a dictionary, you can use a series of colon-separated pairs of expressions (the pairs are the items of the dictionary) separated by commas (,) within braces ({}); You may optionally place a redundant comma after the last item. Each item in a dictionary is written as key:value, where key is an expression giving the item’s key and value is an expression giving the item’s value. If a key’s value appears more than once in a dictionary expression, only an arbitrary one of the items with that key is kept in the resulting dictionary object—dictionaries do not allow duplicate keys. To denote an empty dictionary, use an empty pair of braces.
Here are some dictionary literals:
{'x':42, 'y':3.14, 'z':7} # Dictionary
with three items, str keys
{1:2,
3:4}
# Dictionary with two items, int keys
{1:'za',
'br':23}
# Dictionary with mixed key types
{}
# Empty dictionary
You can also call the built-in type dict to create a dictionary in a way that sometimes be more readable. For example, the dictionaries in the preceding snippet can equivalently be written as:
dict(x=42, y=3.14,
z=7) # Dictionary
with three items, str keys
dict([(1, 2), (3,
4)]) #
Dictionary with two items, int keys
dict([(1,'za'), ('br',23)]) # Dictionary with mixed key types
dict()
# Empty dictionary
dict() without arguments creates and returns an empty dictionary, like {}. When the argument x to dict is a mapping, dict returns a new dictionary object with the same keys and values as x. When x is iterable, the items in x must be pairs, and dict(x) returns a dictionary whose items (key/value pairs) are the same as the items in x. If a key value appears more than once in x, only the last item from x with that key value is kept in the resulting dictionary.
When you call dict, in addition to, or instead of, the positional argument x, you may pass named arguments, each with the syntax name=value, where name is an identifier to use as an item’s key and value is an expression giving the item’s value. When you call dict and pass both a positional argument and one or more named arguments, if a key appears both in the positional argument and as a named argument, Python associates to that key the value given with the named argument (i.e., the named argument "wins").
You can also create a dictionary by calling dict.fromkeys. The first argument is an iterable whose items become the keys of the dictionary; the second argument is the value that corresponds to each and every key (all keys initially map to the same value). If you omit the second argument, it defaults to None. For example:
dict.fromkeys('hello', 2) # same as {'h':2, 'e':2,
'l':2, 'o':2}
dict.fromkeys([1, 2, 3]) # same as
{1:None, 2:None, 3:None}
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Python program to get location meta data from an image
Retrieve Twitter posts and comments using Python
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Python Basics - Setting up your Python Development Environment
Schwartzian Transform in python
Multidimensional list (array) in python
Perl's chomp() equivalent for removing trailing newlines from strings in python