Python has about two dozen data types built in to the interpreter. The three data types that you will need to understand the best and use the most to manage data are the list, tuple, and dictionary.
A list in Python is simply an ordered collection of objects. The objects can be named any legal Python name and the list can grow dynamically to support the addition of new objects. The objects in a list can be of different types and Python will keep track of the data type of objects in the background. Lists in Python are ordered sequence types. Elements of a list are accessible using a zero-based non-negative integer index.
A tuple in one sense is just a read-only version of a list. It is also an ordered sequence of objects. However, a tuple is immutable, meaning that items cannot be added to or removed from it.
A dictionary is an unordered collection of object pairs. The pair consists of a key object and a value object. The key object is used to look up the value of the value object. A dictionary acts similar to a hash table in that the key is used to access the value objects within. There is no order to a dictionary; therefore, items cannot be accessed by any indexing method.
This chapter discusses phrases that allow you to manage data using the list, tuple, and dictionary data types.
Example .
numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["Python", "Phrasebook", ["Copyright", 2006]] listList = [numList, stringList, mixedList, subList]
Defining a list in Python is a simple matter of assigning a number of Python objects to a variable name using the =
operator. The list needs to be enclosed in square brackets and can include any makeup of Python objects. A simple numeric list acts much like an array; however, lists are much more dynamic and can include many different types within the same list.
The code example in def_list.py
demonstrates the creation of both homogeneous and heterogeneous lists. Notice in the example that the lists include numbers, strings, list definitions, and variable names.
numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["Python", "Phrasebook", ["Copyright", 2006]] listList = [numList, stringList, mixedList, subList] for x in listList: for y in x: if isinstance(y, int): print y + 1 if isinstance(y, basestring): print "String:" + y
2001 2004 2006 2007 String: Essential String: Python String: Code 2 3 String: three 5 String: Python String: Phrasebook
Output from def_list.py code
Example .
for x in numList: print x+1 print stringList[0] + ' ' + stringList[1] + ' ' + stringList[2] print stringList[-2] if isinstance(subList, list): print subList[2][0]
Once a list is defined, the items in the list can be accessed using a zero-based index. The first item in the list is at index zero, the second at index one, and so on.
The code example in acc_list.py
demonstrates accessing all items of the list in order using the for
keyword, as well as accessing the items in the list individually.
If an item in the list is a list object, you can access items in that list by adding an indexing bracket onto the end, similar to how you would access elements in a multidimensional array.
Python enables you to use negative indices to access the list from the end rather than from the beginning. For example, to access the final item in a list, you would use an index of −1, an index of −2 to access the second to the last item in the list, and so on. This can be extremely useful if you have dynamic lists that change frequently.
numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["Python", "Phrasebook", ["Copyright", 2006]] listList = [numList, stringList, mixedList, subList] #All items for x in numList: print x+1 #Specific items print stringList[0] + ' ' + stringList[1] + ' ' + stringList[2] #Negative indices print stringList[-2] #Accessing items in sublists if isinstance(subList, list): print subList[2][0]
2001 2004 2006 2007 Essential Python Code Python Copyright
Output from acc_list.py code
Example .
firstHalf = monthList[ : halfCount] secondHalf = monthList[halfCount : ] wordCount = len(firstHalf) middleStart = wordCount/2 middleHalf = monthList[middleStart : middleStart+halfCount]
A slice is a subset of a list. Python provides syntax that enables you to quickly grab specific slices of a list.
A slice can be obtained by referencing the list and specifying two indices (separated by a colon) to reference between instead of a single index number. The first index number represents the item in the list at which to start and the second represents the item in the list at which to end.
Slices are returned as a list type and can be accessed and assigned as you would any other list.
Python enables you to use negative indices to index the end rather than the beginning when grabbing slices. For example, to access the final three items in a list, you would use the indices of −3 and −1.
monthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December"] wordCount = len(monthList) halfCount = wordCount/2 #Beginning slice firstHalf = monthList[ : halfCount] print firstHalf #End slice secondHalf = monthList[halfCount : ] print secondHalf #Middle slice wordCount = len(firstHalf) middleStart = wordCount/2 middleHalf = monthList[middleStart : middleStart+halfCount] print middleHalf #Negative Indices print monthList[-5 : -1]
slice_list.py
['January', 'February', 'March', 'April', 'May', 'June'] ['July', 'August', 'September', 'October', 'November', 'December'] ['April', 'May', 'June', 'July', 'August', 'September'] ['August', 'September', 'October', 'November']
Example .
list1.append("Four") list1.insert(2, "Two 1/2") list1.extend(list2) print list1.pop(2) list1.remove("Five") list1.remove("Six")
Items can be added to an existing list in several different ways, depending on what items you want to add to the list and where you want to add them.
The simplest way to add a single item to a list is to use the append(
item
)
method. append
takes a single item—which can be any Python object, including other lists—as the only parameter and adds it to the end of the list. If you specify a list as the parameter to the append
method, that list is added as a single item in the current list.
Use the extend(
list
)
method to add several items stored in another list all together at the same time. extend
will accept only a list as an argument. Unlike the append
method, each item in the new list will be appended as its own individual item to the old list.
The extend
and append
methods will add items only to the end of the list. Use the insert(
index, item
)
method to insert an item in the middle of the list. The insert
method accepts a single object as the second parameter and inserts it into the list at the index specified by the first argument.
Items can be removed from a list in one of two ways. The first way is to use the pop(
index
)
method to remove the item by its index. The pop
method removes the object from the list and then returns it.
The second way to remove an item from a list is to use the remove(
item
)
method. The remove
method will search the list and remove the first occurrence of the item.
list1 = ["One", "Two", "Three"] list2 = ["Five", "Six"] print list1 #Append item list1.append("Four") print list1 #Insert item at index list1.insert(2, "Two 1/2") print list1 #Extend with list list1.extend(list2) print list1 #Pop item by index print list1.pop(2) print list1 #Remove item list1.remove("Five") list1.remove("Six") print list1 #Operators list1 += list2 print list1
add_list.py
['One', 'Two', 'Three'] ['One', 'Two', 'Three', 'Four'] ['One', 'Two', 'Two 1/2', 'Three', 'Four'] ['One', 'Two', 'Two 1/2', 'Three', 'Four', 'Five', 'Six'] Two 1/2 ['One', 'Two', 'Three', 'Four', 'Five', 'Six'] ['One', 'Two', 'Three', 'Four'] ['One', 'Two', 'Three', 'Four', 'Five', 'Six']
Output from add_list.py code
Example .
def keySort (x, y): xIndex = keyList.index(x) yIndex = keyList.index(y) return cmp(xIndex, yIndex) letterList.sort() letterList.sort(lambda x, y: keySort(x, y)) caseList.sort() caseList.sort(key=str.lower) letterList.reverse() letterList.sort(reverse=1)
Items in a list can be sorted using the sort()
method. The basic sort
method takes no arguments and sorts the items based on the total value of each object. The sort
method actually modifies the order of the objects in the list itself. This works as a simple and very effective way to sort simple lists.
The sort
method can also accept a comparison function as an argument. The comparison function accepts two arguments and must return a 1, 0, or −1 depending on whether the second argument is smaller, the same size, or larger than the first argument.
The sort
method can also accept a key function. The key function should accept one argument that will be used to extract a key from each object in the list. That key will be used to sort the list rather than the value of the object itself.
A list can be sorted in reverse order, by passing the keyterm reverse
as an argument to the sort
method. reverse
is a Boolean, and if it is set to true, the list is sorted in reverse order. The reverse
keyterm can be used in tandem with comparison and/or key functions.
If you simply need to reverse the order of a list without necessarily sorting it, use the reverse()
method. The reverse
method accepts no arguments and simply reverses the order of the items in the list.
keyList = ['a', 'c', 'b', 'y', 'z', 'x'] letterList = ['b', 'c', 'a', 'z', 'y', 'x'] caseList = ['d', 'B', 'F', 'A', 'E', 'c'] #Custom sort procedure def keySort (x, y): xIndex = keyList.index(x) yIndex = keyList.index(y) return cmp(xIndex, yIndex) print letterList #Sort the list letterList.sort() print letterList #Custom sort letterList.sort(lambda x, y: keySort(x, y)) print letterList #Key sort print caseList caseList.sort() print caseList caseList.sort(key=str.lower) print caseList #Reverse list letterList.reverse() print letterList #Reverse sort letterList.sort(reverse=1) print letterList
sort_list.py
['b', 'c', 'a', 'z', 'y', 'x'] ['a', 'b', 'c', 'x', 'y', 'z'] ['a', 'c', 'b', 'y', 'z', 'x'] ['d', 'B', 'F', 'A', 'E', 'c'] ['A', 'B', 'E', 'F', 'c', 'd'] ['A', 'B', 'c', 'd', 'E', 'F'] ['x', 'z', 'y', 'b', 'c', 'a'] ['z', 'y', 'x', 'c', 'b', 'a']
Output from sort_list.py code
Example .
hexStringChars = ('A', 'B','C', 'D', 'E', 'F') hexStringNums = ('1', '2', '3', '4', '5', '6', '7', '8', '9','0') hexStrings = ["1FC", "1FG", "222", "Ten"] for hexString in hexStrings: for x in hexString: if ((not x in hexStringChars) and (not x in hexStringNums)): print hexString+ " is not a hex string." break tupleList = list(hexStringChars) listTuple = tuple(hexStrings)
When working with lists in Python, it is a good idea to understand the place that tuples have. Tuples are similar to lists in that they are index-based collections of objects. There is one major difference, however. The contents of a tuple cannot be modified after the tuple is initially defined. Tuples are defined similar to lists except that they are encased in parentheses instead of in brackets.
Tuples are very valuable because they are much faster to access and use than lists. For example, the in
operation works much faster on a tuple to determine whether an object exists in the tuple. Tuples are also valuable because you know the data contained in them will always remain static. Tuples can also be used as keys for dictionaries where lists cannot.
The tuples must be made up of strings and/or integers and cannot contain lists to be considered immutable and used as dictionary keys.
Tuples can be converted into lists by using the list()
function. The list
function returns a copy of the tuple in an editable list form. In the same way, lists can be converted into tuples using the tuple()
function. The tuple
function returns a copy of the list in tuple form, effectively giving you a frozen snapshot of the list.
hexStringChars = ('A', 'B','C', 'D', 'E', 'F') hexStringNums = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') hexStrings = ["1FC", "1FG", "222", "Ten"] for hexString in hexStrings: for x in hexString: if ((not x in hexStringChars) and (not x in hexStringNums)): print hexString + " is not a hex string." break #Tuple to list tupleList = list(hexStringChars) print tupleList #List to tuple listTuple = tuple(hexStrings) print listTuple
tuple.py
1FG is not a hex string. Ten is not a hex string. ['A', 'B', 'C', 'D', 'E', 'F'] ('1FC', '1FG', '222', 'Ten')
Output from tuple.py code
Example .
numberDict = {1:'one', 2:'two', 3:'three', 4:'four'} letterDict = {'vowel':['a','e','i','o','u'], 'consonant':['b','c','d','f']} numbers = (1,2,3,4,5,6,7,8,9,0) letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]}
Constructing a dictionary in Python is a simple matter of assigning a group of values with associated keys to a variable. Although the values can be any Python object, the keys must either be a number, string, or immutable tuple.
Simple dictionaries are made up of simple one-to-one, key-to-value relationships. However, you can construct very complex dictionaries that can have one-to-many and even many-to-many value relationships.
A one-to-many relationship can be accomplished by simply using list objects as the values in the dictionary.
The many-to-many relationship will take more thought and effort; however, this relationship can be accomplished by using tuples as the key objects and list objects as the value objects in the dictionary.
#Simple one to one dictionary numberDict = {1:'one', 2:'two', 3:'three', 4:'four'} #One to many dictionary letterDict = {'vowel':['a','e','i','o','u'], 'consonant':['b','c','d','f']} #Many to many dictionary numbers = (1,2,3,4,5,6,7,8,9,0) letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]}
Example .
numbers = ('1','2','3','4','5','6','7','8','9','0') letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]} cSet = raw_input("Insert characters: ") for c in cSet: for x in charSetDict.keys(): if c in x: charSetDict[x].append(c) break; charSetDict["Special"] = ['%', '$', '#'] charSetDict["Special"] = '><'
Adding values to a dictionary is really just setting up a key in the dictionary to correspond to a specific value. When assigning a value to the dictionary, if the key you specify does not already exist in the dictionary, the key is added to the dictionary and the value is assigned to it. If the key already exists in the dictionary, the value object currently assigned to the key will be replaced by the new value object.
The object type of the value and key do not need to match, and at any time you can replace the value object with a new object of any type.
Be aware that the keys in the dictionary are case sensitive. For example, Name
and name
would represent two completely distinct keys in the dictionary.
numbers = ('1','2','3','4','5','6','7','8','9','0') letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]} def display_cset (cset): print for x in cset.items(): if x[0] == numbers: print "Numbers:" elif x[0] == letters: print "Letters:" elif x[0] == punct: print "Puctuation:" else: print "Unknown:" print x[1] #Add new values to keys cSet = raw_input("Insert characters: ") for c in cSet: for x in charSetDict.keys(): if c in x: charSetDict[x].append(c) break; display_cset(charSetDict) #Add new key and value charSetDict["Special"] = ['%', '$', '#'] display_cset(charSetDict) #Change value for existing key charSetDict["Special"] = '><' display_cset(charSetDict)
add_dict.py
Insert characters: abc 123 . Numbers: ['1', '2', '3'] Puctuation: ['.'] Letters: ['a', 'b', 'c'] Numbers: ['1', '2', '3'] Puctuation: ['.'] Letters: ['a', 'b', 'c'] Unknown: ['%', '$', '#'] Numbers: ['1', '2', '3'] Puctuation: ['.'] Letters: ['a', 'b', 'c'] Unknown: ><
Output of add_dict.py
Example .
validkeys = (1,2,3) keyGenDict={'keys':[1,2,3],1:'blue',2:'fast', 3:'test','key':2} print keyGenDict.keys() print keyGenDict.values() print keyGenDict.items() val = keyGenDict["key"] keyGenDict["key"] = 1 val = keyGenDict["key"]
A value can be retrieved from a dictionary using several different methods. The most common is to access the value directly by specifying the associated key in square brackets following the dictionary variable.
A list of values contained in a dictionary can be retrieved using the values()
method. The values
method returns a list containing all objects that are values in the dictionary.
Similarly, you can obtain just a list of keys using the keys()
method. The keys
method returns a list of objects that are currently being used as keys in the dictionary. The list of keys is useful in many ways, such as creating a tuple of the keys for faster lookups in the dictionary.
You can also get a list of key and value pairs by using the items()
method. The items
method returns a list that contains two-element tuples of each key and value pair in the dictionary.
validkeys = (1,2,3) keyGenDict={'keys':[1,2,3],1:'blue',2:'fast', 3:'test','key':2} def show_key (key): if(key in validkeys): keyVal = (keyGenDict["keys"])[key-1] print "Key = " + keyGenDict[keyVal] else: print("Invalid key") #Retrieving dictionary key list print keyGenDict.keys() #Retrieving dictionary value list print keyGenDict.keys() #Retrieving dictionary value list print keyGenDict.items() #Retrieve value from key val = keyGenDict["key"] show_key(val) keyGenDict["key"] = 1 val = keyGenDict["key"] show_key(val)
ret_dict.py
['keys', 1, 2, 3, 'key'] [[1, 2, 3], 'blue', 'fast', 'test', 2] [('keys', [1, 2, 3]), (1, 'blue'), (2, 'fast'), (3, 'test'), ('key', 2)] Key = fast Key = blue
Output of ret_dict.py
Example .
year = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'} months = year.keys() months.sort() halfCount = len(months)/2 half = months[0:halfCount] firstHalf = {} for x in half: firstHalf[x] = year[x]
There is no specific method to get a slice of a dictionary; however, this will be a common task that deserves some attention. The best way to slice out a subset of a dictionary is to first get the list of keys using the keys
method. From the full list of keys, create a subset of that list through slicing or whatever means are necessary.
Once you have a specific subset of keys in the directory, you can pull out the values from the original dictionary and add them to a new dictionary.
If you want to keep the original dictionary intact, use the get
method to pull out the value. However, if you want the value and keys removed from the original dictionary, use the pop
method.
year = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'} print year #Get list of keys months = year.keys() #Create subset of keys months.sort() halfCount = len(months)/2 half = months[0:halfCount] #Create new dictionary from subset of keys firstHalf = {} for x in half: firstHalf[x] = year[x] print firstHalf
{1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June'}
Output of sub_dict.py
Example .
myDictionary = {'color':'blue', 'speed':'fast', 'number':1, 5:'number'} swapDictionary = {} for key, val in myDictionary.iteritems(): swapDictionary[val] = key
Currently, there is not a method in Python to swap around the keys and values. However, this can be very useful if you are using a dictionary in which you may frequently need to look up items by value. Rather than searching through the entire dictionary each time, you could create an alternative dictionary that has the values swapped.
To swap the keys and values in a dictionary, simply iterate through the items in the dictionary using the iteritems
method and use the values as keys assigning the original key as the value.
myDictionary = {'color':'blue', 'speed':'fast', 'number':1, 5:'number'} print myDictionary #Swap keys for values swapDictionary = {} for key, val in myDictionary.iteritems(): swapDictionary[val] = key print swapDictionary
swap_dict.py
{'color': 'blue', 'speed': 'fast', 'number': 1, 5: 'number'} {'blue': 'color', 1: 'number', 'number': 5, 'fast': 'speed'}
Output of swap_dict.py