python dictionary
By: Ashley J in Python Tutorials on 2017-09-28
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}
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- 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
Comments