Python 3.6 [conda env: PY36]

Working With Basic Built In Python Data Structures

This document gives quick demo of methods for lists, sets, etc. Dictionaries is handled in a separate document.

TOC:

Working with Lists


In [31]:
tstLst = []  # build an empty list
print(tstLst)


[]

In [32]:
tstLst.append(9)
tstLst.append("Dog")
print(tstLst)


[9, 'Dog']

In [33]:
# adding lists together
tstLst += ["cat", 10, 18, "animal", "mouse"]   
tstLst = tstLst + ["snow", "999", 998]
print(tstLst)


[9, 'Dog', 'cat', 10, 18, 'animal', 'mouse', 'snow', '999', 998]

In [34]:
tstLst.remove("cat") # throws an error if not found
print("tstLst after .remove('cat'): %s" %tstLst)
print("Poping this value: %s" %tstLst.pop())  # pops the last value off the top
print("Popping the 3rd value: %s" %tstLst.pop(2))
print(tstLst)


tstLst after .remove('cat'): [9, 'Dog', 10, 18, 'animal', 'mouse', 'snow', '999', 998]
Poping this value: 998
Popping the 3rd value: 10
[9, 'Dog', 18, 'animal', 'mouse', 'snow', '999']

In [35]:
# to sort it, the list must all be the same type:
tstLst = [str(i) for i in tstLst]
print(tstLst)  # unsorted
tstLst.sort()
print(tstLst)  # note: .sort() mutates and returns None
               # if this cell throws an error, re-run previous cells before re-running it


['9', 'Dog', '18', 'animal', 'mouse', 'snow', '999']
['18', '9', '999', 'Dog', 'animal', 'mouse', 'snow']

In [37]:
# sorted returns a value, this example combines with list comprehensions and string functions
# previous cells concerted all values within the list to strings
sorted([i.lower() for i in tstLst if i.isnumeric() == False])  # note: upper case sorts ahead of lower case


Out[37]:
['animal', 'dog', 'mouse', 'snow']

In [39]:
tstLst.insert(1, "13")  # inserts after index 1 i original list
print(tstLst)


['18', '13', '9', '999', 'Dog', 'animal', 'mouse', 'snow']

In [42]:
tstLst.reverse() # mutates original and returns None
print(tstLst)


['snow', 'mouse', 'animal', 'Dog', '999', '9', '13', '18']

Working with Sets


In [3]:
var1 = {}
def test_var(var):
    print("Type %s: \nContent: %s" %(type(var), var))
test_var(var1)


Type <class 'dict'>: 
Content: {}

In [4]:
testSet = set()  # build an empty set, note that var = {} builds an empty dictionary
test_var(testSet)


Type <class 'set'>: 
Content: set()

In [5]:
testSet.remove(9)  # want an error thrown if you try to remove what does not exist?  Use this one


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-bad50fa3b78f> in <module>()
----> 1 testSet.remove(9)

KeyError: 9

In [43]:
testSet.discard(9)  # does not exist?  Don't want an error?  Use this one

In [44]:
testSet.add(8)
testSet.add(13)
testSet = testSet.union({7,14,15})
print(testSet)


{7, 8, 13, 14, 15}

In [45]:
testSet.intersection({1,2,3,4,5,6,7,8,9})


Out[45]:
{7, 8}

In [ ]:
# for future development ...
# demonstrate difference, symmetric_difference, etc ...