Lists, Tuples, Sets and Dictionaries in Python

Pallavi Krishna
2 min readJul 20, 2021

In Python, we use four types of data structures / containers, Lists, Tuples, Sets and Dictionaries. They are all indexed and it is possible to iterate over all four. Let us see each one in detail.

Lists are very powerful array like structures in Python. The key difference between an array used in any other language and the List in Python is that the List does not need to contain only homogeneous information. For example, it is entirely possible in Python to store a number and string in the same list. Lists are also powerfully accessible via its feature of slicing. It is represented by [] in Python. Some examples of a list are below -

flowers = [‘Rose’, ’Marigold’, ’Sunflower’, ‘Jasmine’, ‘Rose’]

multi = [1, ‘string’, ‘12’ , 301, 1]

Tuple is an immutable collection of objects in Python. What this means is that we cannot change what’s in a tuple. It behaves in all other ways similar to a list. It is represented by () in Python

flowers = (‘Rose’, ‘Marigold’, ‘Sunflower’, ‘Jasmine’)

num = (1, 2, 3, 4, 5, 1, 1)

A Set is a collection of objects in Python that does not have duplicates in it. Important to remember here is that a set in Python is an unordered collection of data. So if I use a set to store the data, I cannot always ensure the data is in the same order as I stored it while retrieving it. It is represented by {} in Python

flowers = {‘Rose’, ‘Marigold’, ‘Sunflower’, ‘Jasmine’}

num = {1, 2, 3, 4, 5}

A Dictionary in Python is a data structure like element that stores any number of key value pairs in it. It is represented by {} in Python. The Keys in a dictionary are immutable and cannot have duplicates.

flowers_trees = {‘Flowers’: [‘Rose’, ‘Marigold’, ‘Sunflower’, ‘Jasmine’], ‘Trees’: [‘Teak’, ‘Sal’, ‘Eucalyptus’]}

Happy Coding!

--

--

Pallavi Krishna

Passionate about programming, data and painting. Open to Work