Why this post?
These three words are very common in Python language. Everyone, who loves Python should have a clean idea on Lists, Tuples and Dictionaries in Python. All of them are used to manage sequences in Python. Python has six built-in types of sequences, but the most common ones are lists, tuples and dictionaries.
Please go through the startup sessions of Python and get a basic idea on how to code Python. Now-a-days we do not have enough time to waste. We all are busy! And we required some automation tools to reduce the work load and save the time.
Python is a good and simple language for this purpose. Bookmark the Index page and read more about Python.
Python Lists
List is the most basic sequence data structure in Python. List a sequence of elements, each elements are referred by a number starting from 0 (zero) called Index number. The first index is zero, the second index is one, and so forth.
The objects/elements in the List is mutable, which means we can change its value at any time.
Defining a Python List
We can define a lists by using square bracket with elements separated by comma.
MyList = [‘Arun’, ‘Lal’, ‘Tom’, ‘Leo’]
That’s it!
Please read the following posts to get more idea on Python lists…
Sequences and Lists – Array concept in Python
More about sequences and Lists – Array concept in Python part 2
Python Tuples
I like to call, it’s immutable lists. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. We can not change the values in a Tuple, once it’s created.
Python Tuples are defined in parentheses(()), whereas lists are defined by using square brackets([]).
Like list, we can simply define a Python tuple in parentheses with comma separated objects.
Examples:
MyTup = ('Arun', 'lal', 'Tom')
Defining an empty tuple:
emtup = ()
We can not change the elements in a tuple once it is created.
Python Dictionary
It’s something different than the concept of Lists and Tuples. It’s a comma separated Key Value pairs. This Key Value pairs are called Items in a Dictionary. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
Please see the example pasted below:
MyDict = {'Name':'Arun', 'Age':28, 'ID':'dErt432'} print(MyDict) {'Age': 28, 'ID': 'dErt432', 'Name': 'Arun'}
Keys are unique within a dictionary while values may not be. Value can repeat for different keys. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
A lot of options are available with Python Dictionary. I will create a separate article to explain this in detail.
One thought on “A quick comparison – Lists, Tuples and Dictionary in Python!”