Skip to content

Instantly share code, notes, and snippets.

@mark-adams
Created December 12, 2013 22:54
Show Gist options
  • Save mark-adams/7937030 to your computer and use it in GitHub Desktop.
Save mark-adams/7937030 to your computer and use it in GitHub Desktop.
Some example python data structures
print 'Storing data using Lists!'
# Notice there are three lists
names = ['Bob', 'Phil', 'Dave']
addresses = ['123 Happy Ln.', '456 Mtn. Rd.', '1000 Century Dr.']
phones = ['111-111-1111', '222-222-2222', '333-333-3333']
# To access data about a person, you reference the same index in all three lists
print '{0} {1} {2}'.format(names[0], addresses[0], phones[0])
print
#########################################################
print 'Storing data using Tuples!'
people = []
# With a tuple, each person's information is grouped together as one tuple object in the list
# The benefit here is that everything is in one list.
people.append(('Bob', '123 Happy Ln', '111-111-1111'))
people.append(('Phil', '456 Mtn. Rd.', '222-222-2222'))
people.append(('Dave', '1000 Century Dr.', '333-333-3333'))
# To reference information, the first index represents which person tuple, the second represents which field in the tuple
print people[0][0]
print people[0][1]
print people[0][2]
print
##########################################################
print 'Storing data using Dictionaries!'
people = []
# Lastly, with dicts each item in the list is a object made up of key/value pairs
# The index is a string representing the key which returns the value for that field
# The benefit here is that you can access the information by name instead of numerical index
people.append({'name':'Bob', 'address': '123 Happy Ln', 'phone':'111-111-1111'})
people.append({'name':'Phil', 'address':'456 Mtn. Rd.', 'phone':'222-222-2222'})
people.append({'name':'Dave', 'address': '1000 Century Dr.', 'phone': '333-333-3333'})
print guy[0]['name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment