Python Data Structures

Python data structures are nothing but a type of collection that allows to organize and manipulate several data values at once. If you are new to the Python programming language and want to know about the inbuilt data structures in Python, then this article is for you. In this article, I will take you through an introduction to Python Data Structures.

Python Data Structures

Lists, Tuples, Sets and dictionaries are the inbuilt data structures in Python. These are nothing but a collection of items used to operate on a collection of values at once. Now let’s go through all these Python data structures one by one to know about what functionality they provide as a data structure in Python.

Lists:

Python list is a collection of items that uses square brackets to enclose items separated by commas. It can be sliced, concatenated, sorted, and mutable. Once you have created a list, you can add more items to it, and you can also replace or remove the items of a Python list. Below is how you can create a Python list:

names = ["Aman", "Akanksha", "Sajid", "Shiwangi", "Aman", "Ashish"]

You can learn more about a Python list from here.

Tuples:

A tuple is an immutable collection of items. It uses parentheses to enclose items separated by commas. If you are storing two values in a variable, then it will become a tuple. All the features of a list and a tuple are the same, but unlike lists, a tuple cannot be modified, as once you have created it, you cannot add, replace, or remove values from it. Below is how you can create a tuple in Python:

items = (12, 15, 17, 54, 98, "aman")

You can learn about Python tuples from here.

Dictionaries:

A dictionary is a collection of key-value pairs enclosed in a set of braces. It is used to associate a unique key with a value where keys are strings or integers and values are any Python objects. You can also store a list of values to a specific key by using a Python list. Below is how you can create a dictionary in Python:

age = {"Aman": 23, "Akanksha": 24, "Sajid": 22}

You can learn more about Python dictionaries from here.

Sets:

A set is an unordered collection of items that cannot store duplicate values. The values inside a Python set are enclosed within braces, just like a dictionary, but unlike dictionaries, sets don’t have any key-value pairs. Below is how you can create a set in Python:

fruits = {"Apple", "Orange", "Mango"}

You can learn more about Python sets from here.

Summary

So lists, tuples, dictionaries, and sets are the inbuilt Python data structures that are used as a collection of items to operate on a collection of values at once. I hope you liked this article on an introduction to Python data structures. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1126

Leave a Reply