From my previous article, I covered the basics and the importance of Data structures and algorithms
For this article we dive into data structures in python.Python is a general purpose programming language that can be used in different fields such as web development,data science and data visualization, machine learning and artificial intelligence, game development.its syntax is very easy to learn given that it uses English-like statements.
There are four built-in data structures in python. These include:
Lists
Sets
Tuples
Dictionaries
LISTS
Lists are containers that can store data of any type. They carry the same concept as arrays in c++ or java only that python doesn't support them.
How is a List created?
By use of square brackets [] and assigning the list a variable name.
myhobbies=['swimming','dancing','climbing']
print(type(myhobbies))
Print Output:
<class 'list'>
Characteristics of a list
- Lists are mutable, meaning they can be changed. Items can be added, removed,or updated in the lists.Let us try to update an item on our list to demonstrate this using indexes.(Recall that we start counting from index [0])
myhobbies=['swimming','dancing','climbing']
myhobbies[1]='baking'
print(myhobbies)
Print Output:
['swimming', 'baking', 'climbing']
2.They are ordered. The particular way in which things were initially arranged cannot be changed.However, there are some methods that can be used to mitigate this.
3.They allow duplicates. We can have similar items in a list.
myhobbies=['swimming','dancing','climbing','swimming']
print(myhobbies)
Print Output:
['swimming', 'dancing', 'climbing', 'swimming']
SETS
This is a collection of unordered items.
How they are created?
Sets are created by use of the set function or using {}.
method 1:
myhobbies={'swimming','dancing','climbing','swimming'}
print(myhobbies)`
Print Output:
<class 'set'>
method 2:
myhobbies=set(['swimming','dancing','climbing'])
print(type(myhobbies))
Print Output:
<class 'set'>
Characteristics of a set
1.They do not allow duplicates.
In the example below, the sets stores two similar hobbies,but on printing,only one 'swimming' is printed.
myhobbies={'swimming','dancing','climbing','swimming'}
print(myhobbies)
Print Output:
{'dancing', 'swimming', 'climbing'}
2.Specific items in a set are immutable,but the set as whole can be changed.
Let's try to add one more hobby into the set.
myhobbies={'swimming','dancing','climbing'}
myhobbies[1]='acting'
print(myhobbies)
We get an error that
TypeError: 'set' object does not support item assignment
3.They are unordered. Meaning that items cannot be accessed using indexes.
TUPLES
They can store a collection of items.They are created by use of parenthesis ().
myhobbies=('swimming','dancing','climbing')
print(type(myhobbies))
Print Output:
<class 'tuple'>
Characteristics of a tuple
- Tuples are ordered,
- They are immutable.
myhobbies=('swimming','dancing','dancing','climbing')
myhobbies[0]='baking'
print((myhobbies))
The following error is raised.TypeError: 'tuple' object does not support item assignment
3.They allow duplicates.
myhobbies=('swimming','dancing','dancing','climbing')
print((myhobbies))
Print Output:
('swimming', 'dancing', 'dancing', 'climbing')
DICTIONARIES
They are used to store data in terms of keys and values. Think of it this way, for one to open a room in their house they have to own a key to that particular room. Similar concept with the dictionary where each value has to have its own unique key.
How they are created?
By use of the curly braces and assigning them a variable name.Like so:
dict={
<key>:<value>
}
where 1 is the key and red is the value
dict={
1:'red',
2:'blue'
}
print (type(dict))
Print Output:
<class 'dict'>
Dictionaries are ordered,they do not allow duplication of keys, and are changeable.
CONCLUSION
It is important to understand the following data structures and their different characteristics to ensure that given different scenarios one can be able to choose the most effective data structure to use based on the expected requirements.
In my next article, we will cover on the different methods that are used for different operations in these data structures.
Top comments (8)
Good job
Thanks!
Good work Joy👏
Thank you!
Cool article
Thank you !
Great article 👌
Thank you so much!