Python Tip #12.5.25 [Data Structures in Python. Strings vs Lists vs Tuples vs Dictionaries]
Skip this paragraph if you want to get straight to the brief tutorial. So for past couple of days I have been trying to learn Python. My love towards this scripting language has slowly grown ever since, even though I was a bit disappointed with the strict indentation that it imposes. Nevertheless, the more I learn about it the more sensible it’s approach becomes or as it’s called the Pythonian approach. But today we shall be looking at the data structures which is crucial when it comes to storing, manipulating datas.
STRINGS, LISTS, TUPLES, DICTIONARIES. Though there are others out there these four data structures are the most widely and commonly used in Python.
What is a string?
- This might be the most common data structure one can find in programming language. Strings are nothing but the values written within quotes. In my last post we talked about how we can use triple quote for multi-line strings so with addition we can also use single and double quotes too. The difference between single and double quotes are that they are often interchangeable but has to be consistent with it. In other words, if we start string with single quote then we must end with single quote regardless of the amount of double quotes used in between.
One important distinction of string in Python with other languages is that strings are immutable, which means that they cannot be changed. They stay constant.
e.g
>>>x = “hello world”
What is a list?
- A list can be viewed as a dynamic array. Unlike arrays in C which must contain only one type for an array, list can have any type i.e. numbers, string functions, objects etc.
e.g.
>>> x = ['hello', 2, 'you']
Lists are mutable. They can be changed.
What are Tuples ?
- In general, tuples are immutable list. Same rule applies to tuples as it did on list too.
e.g.
>>> x = (‘hello’, 2, ‘you’)
Again, tuples are immutable just like strings.
What are Dictionaries?
- Dictionaries are dynamic arrays which has key and value pair and can be either integer, tuple or string. It can compared to the hash table because it behaves like one.
Dictionaries are mutable just like lists.
INITIALIZING
The following are ways to initialize an empty variable for strings, lists, tuples, and dictionaries.
String:- x = ‘’
or
x = “”
or
x = “””
“””
Lists:- y = []
Tuples:- z = ()
Dictionaries:- m = {}
Now that you have created a variable we want to add some values to them. We can achieve this by doing the following:-
INSERTING
Strings:- Like mentioned earlier, strings are immutable. They cannot be changed but that does not mean we cannot insert. There are ways to do it but if you want to be able to keep changing the value then using other data structure is recommended. That being said, here’s one way of inserting values in string.
x = “I need to change”
x = x[:2] + “really” + x[2:] (x = x[:position] + “new variable” + x[position:])
Lists:- Unlike strings lists can be manipulated. Taking the variable “x” from the above we can insert values to list as follows:-
y.append(“add me”) //to add at the end of the list, only one value can be added at a time
y.insert(1,”now”) //to add at the specified index which is 1 in this case, this too can only be done one at a time
y = y + [“or”, “delete”, “me”, 4 , “eva”] //To add multiple values at once at the end of the list
y = y[2:] + [“ignore”,”this”] + y[:2] // To insert multiple values at the index 2
Tuples:-
Tuples, if you read above, are also immutable just like strings. One of the ways to get around this is that you would first convert it into list an then convert it back to tuple.
Taking the variable from above we can insert values in tuples as following:-
z = list(z)
z.append(“inserting in tuple”)
z = tuple(z)
As you can see we are technically inserting to the list, so anything that can be done on the list as mentioned above can be done in tuple too with the extra conversion step. Again, it is not recommended and not a Pythonian approach to change immutable objects but there are workarounds.
Dictionaries:-
Inserting in dictionaries is a bit different than tuples or lists. Because it also requires key for the value to make a pair so we have to specify key for each value. We specify key on the left and value on the right separated by “:”
e.g.
m = {“fruit”:”apple”,”veggie”:”potato”,”fruit”:”grape”}
or
m[“fruit”] = “apple”
m[“veggie”] = “potato”
Some other operations that can be performed:-
There are numerous operations that can be performed in data structures. The above is just a brief introduction to data structures. You can delete, sort, split and other operations that you can perform in data structures. I will leave that up to you to discover. I might talk about one or two in near future but I will leave Python’s documentation to do the explanation this time. Happy Pythoning.

Image Courtesy of XKCD.com
Python’s Documentation / Reference:-

