Python Tutorial 4: Data Structures
Data structures are basically just that – they are structures which can hold some data together. In other words, they are used to store a collection of related data. There are three built-in data structures we often use in Python – list, dictionary and set.
Detailed Explanation(optional)
Set
A set is an unordered collection with no duplicate elements.
This is the same ‘set’ as in math set theory – a collection of distinct objects. Shown with curly braces
my_distinct_set = {3,'two',3,1}
print(my_distinct_set)
As you may notice, they automatically sort in alphanumeric order (numbers first then string) and remove duplicates. They can also be made with the set inbuilt function
my_other_set = set(('a','l','e','x'))
print(my_other_set)
me = set('alex')
print(me)
Methods
Sets can be used to do set theory math operations like union, intersection, difference, symmetric difference
not_me = set('ting wei')
print(not_me)
Difference – In my name but not in his name
difference = me - not_me
print(me - not_me)
Union – In mine or his or both
union = me | not_me
print(union)
Intersection – In both mine and his
intersection = me & not_me
print(intersection)
Symmetric difference In either my name or his but not both
symmetricdifference = me ^ not_me
print(symmetricdifference)
Dictionary
A dictionary is an unordered set of key: value pairs. You can essentially think of it similar to an actual dictionary, with a word and associate meaning/definition of the word
Shown with curly braces and a “:” separator
telephonebook = {'jack': 81244098, 'sape': 92344139}
print(telephonebook)
print(type(telephonebook))
Methods
print(telephonebook.items())
print(telephonebook.keys())
print(telephonebook.values())
telephonebook.clear()
print(telephonebook)
List
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type. This is the most used data structure when you are doing analytics!
Again the builtin function can be used as an alternative to manually using square brackets
sq = [1,4,9,16,25]
print(sq)
sq2 = list((1,4,9,16,25))
print(sq2)
Elements can be accessed via indexing, with the first element with an associated index of 0
newlist = ["this","is","a","string"]
print(newlist[0])
Python conveniently allows for negative indexes, useful for long lists or to skip checking the length of a data structure
#The normal way of getting to the last item of the list
print(newlist[3])
#Alternatively, you could access from the back
print(newlist[-1])
Elements can be edited easily within a list as well through normal assignment of value
newlist = ["this","is","a","string"]
newlist[0] = "that"
print(newlist)
List has many useful methods that we use to process data
#Appending new elements to the list
lst1 = [1,2,3,4,5]
newnumber = 6
lst1.append(newnumber)
newstring = "a string"
lst1.append(newstring)
print(lst1)
#Inserting elements to specific position of the list
lst1.insert(5,"inserted item")
print(lst1)
.insert above displaces the element in the fifth position and inserts a 6 into the list. In programming we start at 0. If you noticed in item assignment for lists above, when i called lst[0] = 12345654, it replaced the first element. The first element is in the zeroth position We can also just call list[index] to return the element at that index