Python Object Types - Dictionaries and Tuples
Dictionaries are not sequences at all. They are mappings. Mappings are collections of objects but they store objects by key instead of by relative position.
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary) and must be of an immutable types, such as a Python string, a number, or a tuple. The value can be of any type including collection of types hence it is possible to create nested data structures.
When we add a key to a dictionary, we must also add a value for that key. Python dictionaries are optimized for retrieving the value when we know the key, but not the other way around. They are very similar to C++'s unordered maps. Dictionaries provide very fast key lookup.
So, mappings don't maintain any reliable left-to-right order. They simply map keys to their associated values. Dictionaries are the only mapping type in Python's core objects set. They are mutable, so they may changed in-place and can grow and shrink on demand.
Dictionaries are coded in curly braces when written as literals. They consist of a series of key: value pairs. Dictionaries are useful when we need to associate a set of values with keys such as to describe the properties of something. For example:
>>> D = {'Composer': 'Brahms', 'period': 'Romantic', 'Symphony': 1}
We can index this dictionary by key to fetch and change the keys' associated values. The dictionary index operation uses the same syntax as that used for sequences, but the item in the square brackets is a key not a relative position:
>>> # Fetch value of key 'Composer' >>> D['Composer'] 'Brahms' >>> >>> # Add 3 to 'Symphony' value >>> D['Symphony'] += 3 >>> D {'period': 'Romantic', 'Symphony': 4, 'Composer': 'Brahms'} >>>
We can build the dictionary in different way starting with an empty dictionary and fills it out one key at a time.
>>> >>> D = {} >>> D['Composer'] = 'Beethoven' >>> D['Period'] = 'Classic' >>> D['Symphony'] = 9 >>> >>> D {'Symphony': 9, 'Period': 'Classic', 'Composer': 'Beethoven'} >>> >>> print (D['Composer']) Beethoven
Let's make the previous example more complicated. We need a first name besides a last name and we want to store several pieces not just the symphony:
>>> >>> D = {'Composer': {'first': 'Johannes', 'last' : 'Brahms'}, 'Period': 'Romantic', 'Piece' : ['Piano Concerto No. 1', 'Piano Concerto No. 2', 'Symphony No. 1', 'Symphony No. 2', 'Violin Concerto in D Major', 'Hungarian Dances'] } >>> D {'Piece': ['Piano Concerto No. 1', 'Piano Concerto No. 2', 'Symphony No. 1', 'Symphony No. 2', 'Violin Concerto in D Major', 'Hungarian Dances'], 'Period': 'Romantic', 'Composer': {'last': 'Brahms', 'first': 'Johannes'}} >>>
Here we have a three-key dictionary at the top: 'Composer', 'Period', and 'Piece'. But the values have become more complicated: a nested dictionary for the name to support multiple parts, and a nested for the piece to list multiple pieces. We can access the components of this structure:
>>> >>> # 'Composer' is a nested dictionary >>> D['Composer'] {'last': 'Brahms', 'first': 'Johannes'} >>> >>> # Index the nested dictionary >>> D['Composer']['last'] 'Brahms' >>> >>> # Piece is a nested list >>> D['Piece'] ['Piano Concerto No. 1', 'Piano Concerto No. 2', 'Symphony No. 1', 'Symphony No. 2', 'Violin Concerto in D Major', 'Hungarian Dances'] >>> >>> # Index the nested list >>> D['Piece'][-1] 'Hungarian Dances' >>> >>> # Expand the Piece list >>> D['Piece'].append('Variations on a Theme by Paganini') >>> D {'Piece': ['Piano Concerto No. 1', 'Piano Concerto No. 2', 'Symphony No. 1', 'Symphony No. 2', 'Violin Concerto in D Major', 'Hungarian Dances', 'Variations on a Theme by Paganini'], 'Period': 'Romantic', 'Composer': {'last': 'Brahms', 'first': 'Johannes'}} >>>
Notice how the last operation here expands the nested piece list. Because the piece list is a separate block of memory from the dictionary that contains it, it can grow and shrink freely.
Let's do some clean up. In Python, when we lose the last reference to object by assigning its variable to something else. For example, all of the memory space occupied by that object's structure is automatically cleaned up for us:
>>> >>> D = 0 >>> D 0 >>>
Python has garbage collection feature. So, the space is reclaimed immediately as soon as the last reference to an object is removed.
We want to construct the following dictionary:
{ 'a': { '1': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '2': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '3': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '4': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '5': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]} }, 'c': { '1': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '2': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '3': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '4': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '5': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]} }, 'b': { '1': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '2': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '3': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '4': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]}, '5': {'count': 0, 'results': [{'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1}]} } }
The code looks like this:
L1 = ['a','b','c'] L2 = ['1','2','3','4','5'] res_list = [ {'grade': 'ok', 'rc': 0}, {'grade': 'warning', 'rc': 1} ] sub_dict = { 'count': 0, 'results': res_list } my_dict = {} for l1 in L1: my_dict[l1] = {} for l1 in L1: for l2 in L2: my_dict[l1][l2] = sub_dict print my_dict
Dictionary only support accessing items by key. They also support type-specific operation with method calls. Since dictionaries are not sequences, they don't maintain any dependable left-to-right order. As we've seen in the previous example, if we print the dictionary back, its keys may come back in a different order than how we typed them.
>>> >>> D = {'a': 97, 'b': 98, 'c': 99} >>> D {'a': 97, 'c': 99, 'b': 98} >>>
What should we do if we want to impose an ordering on a dictionary's items?. One common solution is to grab a list of keys with the dictionary keys method, and sort that with the list sort method. Then, step through the result using for loop:
>>> >>> # Unordered keys list >>> sortKeys = list(D.keys()) >>> sortKeys ['a', 'c', 'b'] >>> # Sorted keys list >>> sortKeys.sort() >>> sortKeys ['a', 'b', 'c'] >>> >>> # Iterate through sorted keys >>> for k in sortKeys: print (k, '=>', D[k]) a => 97 b => 98 c => 99 >>>
This is a three-step process, though. We can do it with built-in sorted() in one step:
>>> >>> D {'a': 97, 'c': 99, 'b': 98} >>> for k in sorted(D): print (k, '=>', D[k]) a => 97 b => 98 c => 99 >>>
The for loop is a simple and efficient way to step through all the items in a sequence and run a block of code for each item in turn.
There is another loop, while:
>>> for c in 'blah!': print(c.upper()) B L A H !
The while loop is a more general sort of looping tool, not limited to stepping across sequences:
>>> >>> x = 5 >>> while x > 0: print('blah!' * x) x -= 1 blah!blah!blah!blah!blah! blah!blah!blah!blah! blah!blah!blah! blah!blah! blah! >>>
The for loop looks like the list comprehension. Both will work on any object that follows the iteration protocol. The iteration protocol mean:
- A physically stored sequence in memory
- An object that generated on item at a time in the context of an iteration operation.
An object falls into this category if it responds to the iter built-in with an object that advances in response to next.
The generator comprehension expression is such an object.
Actually, every Python tool that scans an object from left to right uses the iteration protocol. And this is why sorted call used in the previous section works on dictionary directly:
for k in sorted(D): print (k, '=>', D[k])
We don't have to call the keys method to get a sequence because dictionaries are iterable object, with a next that returns successive keys.
This also means that any list comprehension expression which computes the cubics like the following example:
>>> cubics = [ x ** 3 for x in [1, 2, 3, 4, 5]] >>> cubics [1, 8, 27, 64, 125] >>>
can always be coded as an equivalent for loop that builds the result list manually by appending as it goes:
>>> for x in [1, 2, 3, 4, 5]: cubics.append(x **3) >>> cubics [1, 8, 27, 64, 125] >>>
The list comprehension, though, and related functional programming tools like map and filter, will generally run faster than a for loop.
If we ever need to tweak code for performance, Python includes tools to help us out such as time and timeit modules and the profile module.
Although we can assign to a new key to expand a dictionary, fetch a key that does not exist is still a mistake:
>>> >>> D = {'A': 65, 'B': 66, 'C': 67} >>> D {'A': 65, 'C': 67, 'B': 66} >>> # Assigning new keys >>> D['E'] = 69 >>> D {'A': 65, 'C': 67, 'B': 66, 'E': 69} >>> D['F'] Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> D['F'] KeyError: 'F' >>> </module></pyshell#40>
The dictionary in membership expression allows us to query the existence of a key and branch on the result with a if statement.
>>> >>> 'F' in D False >>> >>> if not 'F' in D: print('missing') missing >>>
There are other ways to create dictionaries and avoid accessing nonexistent keys. The get method and the try statement, and if/else expression.
>>> >>> D {'A': 65, 'C': 67, 'B': 66, 'E': 69} >>> >>> value = D.get('x', 0) >>> value 0 >>> >>> value = D['x'] if 'x' in D else 0 >>> value 0 >>>
The tuple object ( reads like toople or tuhple) is immutable list. It is an ordered sequence of zero or more object references. Tuples are coded in parentheses instead of square brackets and they support arbitrary types, arbitrary nesting, and the usual sequence operations such as len() as well as the same slicing syntax.
A tuple is much like a list except that it is immutable (unchangeable) once created. Since tuples are immutable, we cannot replace or delete any of items. So, if we want to modify an ordered sequence, we need to use a list instead of a tuple, or if we already have a tuple but want to modify it, all we need is to just convert it to a list, and then apply the changes we want to make.
>>> empty = () >>> type(empty) <class 'tuple'> >>> t = ("1") >>> type(t) <class 'str'> >>> t = ("1",) >>> type(t) <class 'tuple'>
Above example shows how to construct tuples. Note that to create a one item tuple, we should use a comma to distinguish it from an expression with ().
>>> >>> # 4-tiem tuple >>> T = (1, 2, 3, 4) >>> len(T) 4 >>> T + (5, 6) (1, 2, 3, 4, 5, 6) >>> >>> # Indexing, slicing etc. >>> T (1, 2, 3, 4) >>> T[1:] (2, 3, 4) >>> T[0:] (1, 2, 3, 4) >>> T[:-1] (1, 2, 3) >>>
Tuples have two type-specific callable methods:
>>> T = ('a','b','c','d','e','f','b','c','b') >>> # Tuple methods: 'f' appears at offset 5 >>> T.index('f') 5 >>> # 'b' appears three times >>> T.count('b') 3
The primary distinction for tuples is that they cannot be changed once created. That is, they are immutable sequences:
>>> # Tuples are immutable >>> T[0] = 'A' Traceback (most recent call last): File "<pyshell#85>", line 1, in <module> T[0] = 'A' TypeError: 'tuple' object does not support item assignment >>> </module></pyshell#85>
Like lists and dictionaries, tuples support mixed types and nesting, but they don't grow and shrink because they are immutable:
>>> >>> T =(3, 'mixed', ['A', 3, 2]) >>> T[1] 'mixed' >>> T[2][0] 'A' >>> T.append(44) Traceback (most recent call last): File "", line 1, in T.append(44) AttributeError: 'tuple' object has no attribute 'append' >>>
So, why do we have a type that is like a list but supports fewer operations?
Immutability!
Immutability is the whole point!
If we pass a collection of objects around our program as a list, it can be changed anywhere. But if we use a tuple, it cannot be changed. In other words, tuples provide a sort of integrity constraint that is convenient in programs of large size.
Tuple packing is:
>>> T = 1, 'a', [{'NAME':'Serah', 'Age':25}] >>> T (1, 'a', [{'Age': 25, 'NAME': 'Serah'}]) >>>
Tuple unpacking for assignment is:
>>> x,y,z = T >>> x,y,z (1, 'a', [{'Age': 25, 'NAME': 'Serah'}]) >>>
Tuple unpacking requires that the list of variables on the left has the same number of elements as the length of the tuple.
Python tutorial
Python Home
Introduction
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Subprocess Module
Regular Expressions with Python
Regular Expressions Cheat Sheet
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
Functions lambda
Built-in Functions
map, filter, and reduce
Decorators
List Comprehension
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
generator.send() method
Iterators
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
argparse
Exceptions
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Closure
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Multithreading ...
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Coding Questions I
Python Coding Questions II
Python Coding Questions III
Python Coding Questions IV
Python Coding Questions V
Python Coding Questions VI
Python Coding Questions VII
Python Coding Questions VIII
Python Coding Questions IX
Python Coding Questions X
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Matplotlib
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Celluar Automata
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization