Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Types of Data Structures in Python: List, Tuple, Sets & Dictionary

Updated on 27 February, 2024

11.22K+ views
7 min read

Python is an all-time favourite language for all Data Science enthusiasts. The versatile nature and easy-to-understand approach help developers to focus more on understanding the trends in the data and deriving meaningful insights rather than spending time to fix a minor semicolon bug or closing the overhead bracket. Python being the most popular language among beginners is adapted quickly, so it becomes important to hold a good grasp of this language. 

Data Structures is an essential concept in any programming language. It defines how the variables and data can be stored and retrieved from the memory in the best possible way, depending upon the data type. It also defines the relationship between variables, which helps in deciding the operations and functions that should be performed over them. Let’s understand how Python manages data.

Types of Data Structure in Python

1. List

This is the simplest and commonly used Data Structure in Python programming. As the name suggests, it is a collection of items to be stored. The items stored can be of any type numeric, string, boolean, objects, etc which makes it heterogeneous. This means that a list can have any type of data and we can iterate over this list using any type of loop.

The elements stored are usually associated with an index that defines the position in the list. The index numbering starts from zero. The list is mutable, meaning elements in the list can be added, removed, or changed even after their definition. This data structure is like arrays in other languages which is usually homogeneous, meaning only one type of data can be stored in arrays. Some basic operations on Lists are as below:

  • To declare a list in Python, put it in the square brackets:

sample_list = [‘upGrad’, ‘1’, 2]

  • To initialize an empty list:

sample_list = list()

  • Add elements to the list:

sample_list.append(‘new_element’) 

  • Remove elements from the list:

sample_list.remove(<element name>) removes the specific element

del sample_list[<element  index num>] removes the element at that index

sample_list.pop(<element  index num>) removes the element of that index and returns that removed element

  • To change element at any index:

sample_list[<any index>] = new item

  • Slicing: This is an important feature that can filter out items in the list in particular instances. Consider that you require only a specific range of values from the list, then you can simply do this by:

sample_list[start: stop: step] where step defines the gap between the elements and by default it is 

Learn about: How to Create Perfect Decision Tree

2. Tuple

This is another data structure that sequentially stores data, meaning that the data added remains in an orderly fashion like the lists. Following the same lines, Tuple can also store heterogeneous data, and the indexing remains the same.

The major difference between the two is that the elements stored in the tuple is immutable and can’t be changed after definition. This means that you cannot add new elements, change existing items, or delete elements from the tuple. Elements can only be read from it via indexing or unpacking with no replacement. 

This makes tuple fast as compared to the list in terms of creation. The tuple is stored in a single block of memory but a list requires two blocks, one is fixed-sized and the other is variable-sized for storing data. One should prefer a tuple over a list when the user is sure that the elements to be stored don’t require any further modification. Some things to consider while using a tuple:

  • To initialize an empty tuple:

sample_tuple = tuple()

  • To declare a tuple, enclose the items in circular brackets:

sample_tuple = (‘upGrad’, ‘Python’, ‘ML’, 23432)

  • To access the elements of the tuple:

sample_tuple[<index_num>] 

3. Sets

In mathematics, a set is a well-defined collection of unique elements that may or may not be related to each other. In tuple and list, one can store many duplicate elements with no-fail, but the set data structure only takes in unique items.

The elements of a set are stored in an unorderly fashion meaning the items are randomly stored in the set and there is no definite position or index supported, neither slicing is allowed in a set. The set is itself mutable but the elements must be immutable because the way sets work are hashing these elements and in this process, only immutable elements can be hashed.

Elements can be added or removed from the set but cannot be changed as there is no concept of indexing and therefore elements can be changed. Like in mathematics, here also all the set operations can be performed such as union, intersection, difference, disjoint. Let’s look at how to implement it:

  • To initialize an empty set:

sample_set = set()

  • Add elements to the set:

sample_set.add(item) This adds a single item to the set

sample_set.update(items) This can add multiple items via a list, tuple, or another set

  • Remove elements from the set:

sample_set.discard(item) Removes element without warning if element not present 

sample_set.remove(item) Raises an error if the element to be removed is not present.

  • Set operations (Assume two sets initialized: A and B):

A | B or A.union(B):  Union operation 

A & B or A.intersection(B): Intersection operation 

A – B or A.difference(B): Difference of two sets

A ^ B or A.symmetric_difference(B) : Symmetric difference of sets

Check out: Data Frames in Python

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

4. Dictionary

This is the most useful data structure in Python, which allows the data elements to be stored in a key-value pair fashion. The key must be an immutable value, and the value can be a mutable item. This concept is like what an actual dictionary looks like, where we have the words as keys and their meanings as values. A dictionary stores these pairs in an unordered fashion, and therefore there is no concept of the index in this data structure. Some important things related to this:

  • To initialize an empty dictionary:

sample_dict = dict()

  • To add elements to the dictionary:

sample_dict[key] = value 

Another way to do this is sample_dict = {key: value}

If you print this dictionary, the output would be: {‘key1’: value, ‘key2’: value … }

  • To get the keys and values of the dictionary:

sample_dict.keys(): returns keys list

sample_dict.values(): returns values list

sample_dict.items(): returns the view object of key-value pairs as tuple in list

Learn data science courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

It’s important to grasp the basic knowledge of data structures in Python. Being in the Data industry, different Data Structures can help to get a better workaround of the underlying algorithms. It makes the developer more aware of the best coding practices to get the results efficiently. The usage of each data structure is highly situation based and requires rigorous practice.

Check out the trending Python Tutorial concepts in 2024

Frequently Asked Questions (FAQs)

1. What is the importance of data structures?

Data structures are one of the foundational pillars of any programming language. They define how the data will be stored and manipulated in the memory. The concepts of data structures remain the same no matter which programming language we are talking about.

The most common data structures include arrays, lists, stacks, queues, trees, hashmaps, and graphs. Some of them are built-in while others need to be implemented by the user with the help of the pre-defined data structures.

2. How can I develop a strong grasp of data structures?

The fundamental concepts of the implementations and working of any data structure should be the first step you should take. After getting familiar with the theoretical concepts and working, you can start with the coding part.

You should always study the time complexities and space complexities of any algorithm or data structure that you are working upon. This will give you a proper insight into the concept, and you will be able to solve any question which requires that particular data structure.

3. When is a Python list preferred for storing data?

A list can be used to store various values with different data types and can be accessed just by their respective indices. When you need to perform mathematical operations over the elements, a list can be used since it allows you to mathematically operate the elements directly.

Since a list can be resized, it can be used to store the data when you are not certain about the number of elements to be stored.