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

4 Built-in Data Structures in Python: Dictionaries, Lists, Sets, Tuples

Updated on 08 January, 2024

5.94K+ views
9 min read

In this article, we’ll be focusing on the data structures in Python and help you in understanding the topic clearly. You’ll find out what they are and how they function. We’ve also shared numerous examples to ensure you don’t have any doubts regarding any topics we’ve shared here. 

So, without further ado, let’s get started. 

What are Data Structures?

Data structures let you organize and manage your data effectively and efficiently. They enhance the accessibility of your data. Modifying the stored data becomes quite more straightforward as well if you have suitable data structures in place. They enable you to organize and store the data so you can perform operations on the same later on without facing any kind of difficulties. 

Python has two types of data structures. The data structures Python supports implicitly are Set, Dictionary, List, and Tuple. These are the built-in data structures of Python. 

Then there are data structures you can create yourself to have better control over the functionality. These are user-defined data structures, and they include Linked Lists, Graphs, Trees, Stacks, Queues, and HashMaps. The user-defined data structures are available in other programming languages as well. 

Read more: 6 Most Commonly Used Data Structures in R

Built-in Data Structures in Python

Python has multiple built-in data structures. These integrated data structures help you in solving the programming problems fast and with much ease. As we mentioned earlier, Python has the following integrated data structures:

  • Dictionaries
  • Lists
  • Sets
  • Tuples

Let’s discuss each one of them in detail:

1. Dictionaries

We use dictionaries to store key-value pairs. Just like a physical dictionary has a word stored along with its meaning, a dictionary in Python stores key-value pairs. The terms are the keys, whereas the various meanings associated with those words are the values. With the value, you can access the keys. 

You can create a dictionary with flower braces. You can also use the dict() function for this purpose. Here is an example:

 my_dict = {} #empty dictionary

print(my_dict)

my_dict = {1: ‘A’, 2: ‘B’} #dictionary with elements

print(my_dict)

The output of the above code:

{}

{1: ‘A’, 2: ‘B’}

Our learners also read: Free online python course for beginners!

You can change the values of the dictionary through the keys. You’d have first to access the keys to change the values. Once you’ve located the keys, you just have to add the required key-value pair for getting the desired result. 

 my_dict = {‘First’: ‘A’, ‘Second’: ‘B’}

print(my_dict)

my_dict[‘Second’] = ‘C++’ #changing element

print(my_dict)

my_dict[‘Third’] = ‘Ruby’ #adding key-value pair

print(my_dict)

The output of the above code:

{‘First’: ‘A’, ‘Second’: ‘B’}

{‘First’: ‘A’, ‘Second’: ‘C’}

{‘First’: ‘A’, ‘Second’: ‘C’, ‘Third’: ‘D’}

You can delete the values in your dictionary by using the pop() function. The pop() function returns the value that you had deleted. You can retrieve a key-value pair through the popitem() function. It returns the tuple of the pair. You can clear the whole dictionary as well by using the clear() function. Here’s the example:

 my_dict = {‘First’: ‘A’, ‘Second’: ‘B’’, ‘Third’: ‘C’}

a = my_dict.pop(‘Third’) #pop element

print(‘Value:’, a)

print(‘Dictionary:’, my_dict)

b = my_dict.popitem() #pop the key-value pair

print(‘Key, value pair:’, b)

print(‘Dictionary’, my_dict)

my_dict.clear() #empty dictionary

print(‘n’, my_dict)

The output of the above code:

Value: C

Dictionary: {‘First’: ‘A’, ‘Second’: ‘B’}

Key, value pair: (‘Second’, ‘B’)

Dictionary {‘First’: ‘A’}

{}

Also read: Python Project Ideas and Topics

2. Lists

We use lists to store data sequentially. Every element of the list has an address, which is also called an index. The index value of a list goes from 0 to the last element present in your list, and its name is positive indexing. Similarly, when you go back from the last element to the first one and count from -1, it’s called negative indexing. 

You can create a list by using square brackets and add elements into them as you require. If you leave the brackets empty, the list wouldn’t have any elements, and it would be empty as well. Here’s an example of a list:

 my_list = [] #create empty list

print(my_list)

my_list = [A, B, C, ‘example’, Z] #creating list with data

print(my_list)

The output of the above code:

[]

[A, B, C, ’example’, Z]

You can add elements to your list by using the insert(), extent(), and append() functions. The insert() function adds those elements which were passed to the index value. The insert() function increases the size of the list as well. 

With the append() function, you can add all the elements passed to it as a single element. On the other hand, the extend() function can add the elements one-by-one. 

Here is an example:

 my_list = [A, B, C]

print(my_list)

my_list.append([555, 12]) #add as a single element

print(my_list)

my_list.extend([234, ‘more_example’]) #add as different elements

print(my_list)

my_list.insert(1, ‘insert_example’) #add element i

print(my_list)

Output of the above code:

[A, B, C]

[A, B, C, [555, 12]]

[A, B, C, [555, 12], 234, ‘more_example’]

[A, ‘insert_example’, B, C, [555, 12], 234, ‘more_example’]

While working with lists, you would encounter the need to remove some elements as well. You can use the ‘del’ keyword. It’s a built-in keyword of Python and it doesn’t return anything back. If you want an element back, you’d have to use the pop() function and to remove an element through its value, you’ll have to use the remove() function. Here’s the example:

 my_list = [A, B, C, ‘example’, Z, 10, 30]

del my_list[5] #delete element at index 5

print(my_list)

my_list.remove(‘example’) #remove element with value

print(my_list)

a = my_list.pop(1) #pop element from list

print(‘Popped Element: ‘, a, ‘ List remaining: ‘, my_list)

my_list.clear() #empty the list

print(my_list)

The output of the above code:

[A, B, C, ‘example’, Z, 30]

[A, B, C, Z, 30]

Popped Element: 2 List remaining: [A, C, Z, 30]

[]

You can pass the index values in Python and get the required values. 

 my_list = [A, B, C, ‘example’, Z, 10, 30]

for element in my_list: #access elements one by one

    print(element)

print(my_list) #access all elements

print(my_list[3]) #access index 3 element

print(my_list[0:2]) #access elements from 0 to 1 and exclude 2

print(my_list[::-1]) #access elements in reverse

The output of the above code:

A

B

C

example

Z

10

30

[A, B, C, ‘example’, Z, 10, 30]

example

[A, B]

[30, 10, Z, ‘example’, 3, 2, 1]

3. Sets

A collection of unique and unordered items is called a set. So, if you repeat the data due to some reason, it would appear in the set only once. Sets in Python are similar to the sets you read about in mathematics. From their properties to their functions, you’ll find plenty of similarities among them.

You can create a set by using the flower braces and passing its values. Here’s an example:

 my_set = {A, B, C, D, E, E, E} #create set

print(my_set)

The output of the above code:

{A, B, C, D, E}

Like we mentioned earlier, you can perform all the functions of sets you perform in arithmetics in Python’s sets. With the union() function, you can combine the data present in two sets. The intersection() function gives you the data that’s present in both of the mentioned sets. 

You have the difference() function that lets you delete the data available in both of the sets and gives you the data which isn’t common among them. The symmetric_difference() function gives you the data remaining in those sets. 

 my_set = {A, B, C, D}

my_set_2 = {C, D, E, F}

print(my_set.union(my_set_2), ‘———-‘, my_set | my_set_2)

print(my_set.intersection(my_set_2), ‘———-‘, my_set & my_set_2)

print(my_set.difference(my_set_2), ‘———-‘, my_set – my_set_2)

print(my_set.symmetric_difference(my_set_2), ‘———-‘, my_set ^ my_set_2)

my_set.clear()

print(my_set)

The output of the above code:

{A, B, C, D, E, F} ———- {A, B, C, D, E, F}

{C, D} ———- {C, D}

{A, B} ———- {A, B}

{A, B, E, F} ———- {A, B, E, F}

set()

Also read: Python Developer Salary in India

upGrad’s Exclusive Data Science Webinar for you –

How upGrad helps for your Data Science Career?

4. Tuples

Tuples are similar to lists, but once you enter data in a tuple, you can’t change it unless it is mutable. Understanding them can be a little tricky, but don’t worry, our example code might help you in that regard. You can create a tuple with the help of the tuple() function. 

 my_tuple = (A, B, C) #create tuple

print(my_tuple)

The output of the above code:

(A, B, C)

The method for accessing values in tuples is the same as in lists. 

 my_tuple2 = (A, B, C, ‘Upgrad’) #access elements

for x in my_tuple2:

    print(x)

print(my_tuple2)

print(my_tuple2[0])

print(my_tuple2[:])

The output of the above code:

A

B

C

Upgrad

(A, B, C, ‘Upgrad’)

A

(A, B, C, ‘Upgrad’)

Conclusion

Now you must’ve grown familiar with the various data structures in Python. We hope you found this article useful. Data structures play a significant role in helping you with organizing, managing, and accessing your data. There are plenty of options to choose from, and each one of them has its particular uses.

Learn Data Science Courses online at upGrad

If you want to find out more about Python and data structures, you should take a look at our courses. 

If you are curious to learn about python, everything about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Check out all trending Python tutorial concepts in 2024.

Frequently Asked Questions (FAQs)

1. When is the dictionary data type used?

Dictionary can be defined as a valuable data type being used in Python as a collection of data that is stored in the pairs of keys and values. Now, values of the dictionary can be of any data type, but keys have to be an immutable data type like tuple, integer, or string.
Dictionaries are found to be pretty handy when you wish to find a value instantly without going through the entire collection of data. Other than that, a dictionary is also very useful when the order of data is not important. Dictionary data type is preferred when memory consideration is not an important factor because they tend to take up more space as compared to lists and tuples.

2. What is the difference between lists and tuples?

List and tuples are both sequence data types that are useful for storing the collection of data in Python. The most significant difference between both of them is that lists are mutable, while tuples are immutable. On top of that, the implication of iterations is much faster in tuples as compared to that in lists.
If you wish to perform insertion and deletion operations, then lists make it easy for you. At the same time, elements can be accessed better in tuples. Another important factor that determines the use of any data structure is memory. Here, lists tend to consume more memory as compared to tuples.

3. How are sets different from lists?

Lists are considered to be the most powerful tool in Python as they don’t always have to be homogeneous. Both lists and sets can contain all sorts of data types. Now, lists can contain duplicate elements, but sets can never contain duplicates, and they will simply disappear from the set.
The order of elements is maintained in lists, but sets can never maintain the order of elements. As lists take up plenty of computational space, they end up taking a huge amount of time. On the other hand, sets make use of hashing for performing look-ups and end up being way faster than lists.