Title: Data Structure Basics
Slug: data_structure_basics
Summary: Data Structure Basics
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Lists

"A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list." - A Byte Of Python

Lists are mutable.


In [1]:
# Create a list of countries, then print the results
allies = ['USA','UK','France','New Zealand',
          'Australia','Canada','Poland']; allies


Out[1]:
['USA', 'UK', 'France', 'New Zealand', 'Australia', 'Canada', 'Poland']

In [2]:
# Print the length of the list
len(allies)


Out[2]:
7

In [3]:
# Add an item to the list, then print the results
allies.append('China'); allies


Out[3]:
['USA',
 'UK',
 'France',
 'New Zealand',
 'Australia',
 'Canada',
 'Poland',
 'China']

In [4]:
# Sort list, then print the results
allies.sort(); allies


Out[4]:
['Australia',
 'Canada',
 'China',
 'France',
 'New Zealand',
 'Poland',
 'UK',
 'USA']

In [5]:
# Reverse sort list, then print the results
allies.reverse(); allies


Out[5]:
['USA',
 'UK',
 'Poland',
 'New Zealand',
 'France',
 'China',
 'Canada',
 'Australia']

In [6]:
# View the first item of the list
allies[0]


Out[6]:
'USA'

In [7]:
# View the last item of the list
allies[-1]


Out[7]:
'Australia'

In [8]:
# Delete the item in the list
del allies[0]; allies


Out[8]:
['UK', 'Poland', 'New Zealand', 'France', 'China', 'Canada', 'Australia']

In [9]:
# Add a numeric value to a list of strings
allies.append(3442); allies


Out[9]:
['UK', 'Poland', 'New Zealand', 'France', 'China', 'Canada', 'Australia', 3442]

Tuples

"Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list." - Python Documentation

"Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences." - StackOverflow

Parentheses are optional, but useful.


In [10]:
# Create a tuple of state names
usa = ('Texas', 'California', 'Maryland'); usa


Out[10]:
('Texas', 'California', 'Maryland')

In [11]:
# Create a tuple of countries
# (notice the USA has a state names in the nested tuple)
countries = ('canada', 'mexico', usa); countries


Out[11]:
('canada', 'mexico', ('Texas', 'California', 'Maryland'))

In [12]:
# View the third item of the top tuple
countries[2]


Out[12]:
('Texas', 'California', 'Maryland')

In [13]:
# View the third item of the third tuple
countries[2][2]


Out[13]:
'Maryland'

Dictionaries

"A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name." - A Byte Of Python


In [14]:
# Create a dictionary with key:value combos
staff = {'Chris' : 'chris@stater.org',
         'Jake' : 'jake@stater.org',
         'Ashley' : 'ashley@stater.org',
         'Shelly' : 'shelly@stater.org'
        }

In [15]:
# Print the value using the key
staff['Chris']


Out[15]:
'chris@stater.org'

In [16]:
# Delete a dictionary entry based on the key
del staff['Chris']; staff


Out[16]:
{'Ashley': 'ashley@stater.org',
 'Jake': 'jake@stater.org',
 'Shelly': 'shelly@stater.org'}

In [17]:
# Add an item to the dictionary
staff['Guido'] = 'guido@python.org'; staff


Out[17]:
{'Ashley': 'ashley@stater.org',
 'Guido': 'guido@python.org',
 'Jake': 'jake@stater.org',
 'Shelly': 'shelly@stater.org'}

Sets

Sets are unordered collections of simple objects.


In [18]:
# Create a set of BRI countries
BRI = set(['brazil', 'russia', 'india'])

In [19]:
# Is India in the set BRI?
'india' in BRI


Out[19]:
True

In [20]:
# Is the US in the set BRI?
'usa' in BRI


Out[20]:
False

In [21]:
# Create a copy of BRI called BRIC
BRIC = BRI.copy()

In [22]:
# Add China to BRIC
BRIC.add('china')

In [23]:
# Is BRIC a super-set of BRI?
BRIC.issuperset(BRI)


Out[23]:
True

In [24]:
# Remove Russia from BRI
BRI.remove('russia')

In [25]:
# What items are the union of BRI and BRIC?
BRI & BRIC


Out[25]:
{'brazil', 'india'}