Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Relative sorted order in Matrix
Next article icon

Ordered Vs Unordered In Python

Last Updated : 08 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, data structures are used to store and organize data efficiently. One of the fundamental differences between these data structures is whether they maintain the order of elements or not. Ordered data structures guarantee that elements are retrieved in the same order they were inserted while unordered data structures do not maintain any specific order.

This article explores the concepts of ordered and unordered data structures in Python, comparing their characteristics, use cases, and examples.

Ordered Data Structures

An ordered data structure is one where the order of elements is preserved. This means that when elements are inserted into the structure, they retain their insertion order when you retrieve them. You can access elements by their position (index).

Examples of Ordered Data Structures:

  • List: A mutable, ordered collection that allows indexing, slicing and changing elements.
  • Tuple: An immutable ordered collection.
  • String: An immutable ordered collection of characters.
  • OrderedDict (from collections module): A dictionary that maintains insertion order of key-value pairs.
Python
# List Example a = [1, 2, 3, 4, 5] a[2] = 10  # Update an element print(a)    # Tuple Example tup = (1, 2, 3, 4, 5)  # Tuples are immutable, so we can't modify them, but we can access elements by index print(tup[2])   

Output
[1, 2, 10, 4, 5] 3 

Unordered Data Structures

An unordered data structure does not preserve the order of elements. Elements in such structures are stored in an unpredictable sequence. These data structures are often used when you need to ensure uniqueness of elements or require fast membership tests.

Examples of Unordered Data Structures:

  • Set: An unordered collection of unique elements. Does not allow duplicates.
  • frozenset: An immutable version of a set.
  • Dictionary (dict): Although dictionaries preserve insertion order starting from Python 3.7, their keys are unordered, meaning you cannot index or slice them like ordered structures.
Python
# Set Example s = {1, 2, 3, 4, 5} s.add(6)  # Add an element print(s) 

Output
{1, 2, 3, 4, 5, 6} 

Comparison Table: Ordered vs Unordered

FeatureOrdered Data StructuresUnordered Data Structures
Order of ElementsElements are stored and retrieved in the order of insertion.No guaranteed order of elements.
IndexingAllows indexing (e.g., arr[0] for lists).Does not allow direct indexing or slicing.
MutabilityCan be mutable (e.g., lists) or immutable (e.g., tuples).Mutable (e.g., sets) or immutable (e.g., frozensets).
DuplicatesAllows duplicates (e.g., lists).Does not allow duplicates (e.g., sets).
Use CasesWhen the order of elements matters (e.g., sequencing, tasks).When uniqueness is more important than order (e.g., fast membership checking).
ExamplesLists, Tuples, Strings, OrderedDict.Sets, frozensets, Dictionaries (prior to Python 3.7).



Next Article
Python | Relative sorted order in Matrix

A

anuragtriarna
Improve
Article Tags :
  • Python
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Ordered Set - Python
    An ordered set is a collection that combines the properties of both a set and a list. Like a set, it only keeps unique elements, meaning no duplicates are allowed. Like a list, it keeps the elements in the order they were added. In Python, the built-in set does not maintain the order of elements, wh
    3 min read
  • OrderedDict in Python
    An OrderedDict is a dictionary subclass that remembers the order in which keys were first inserted. The only difference between dict() and OrderedDict() lies in their handling of key order in Python. OrderedDict vs dict in PythonNote: Starting from Python 3.7, regular dictionaries (dict) also mainta
    10 min read
  • Is List Ordered in Python
    Yes, lists are ordered in Python. This means that the order in which elements are added to a list is preserved. When you iterate over a list or access its elements by index, they will appear in the same order they were inserted. For example, in a Python list: The first element is always at index 0.T
    1 min read
  • Python | Relative sorted order in Matrix
    Sometimes, while working with Python Matrix, we can have data arranged randomly and we can have a requirement in which we need to get the element position in sorted order of Matrix. Let's discuss a certain way in which this task can be performed. Method : Using list comprehension + enumerate() + sor
    4 min read
  • Python | Pandas Index.sort_values()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.sort_values() function is used to sort the index values. The function ret
    2 min read
  • Python | Pandas Series.sort_index()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.sort_index() function is used
    2 min read
  • Priority Queue in Python
    A priority queue is like a regular queue, but each item has a priority. Instead of being served in the order they arrive, items with higher priority are served first. For example, In airlines, baggage labeled “Business” or “First Class” usually arrives before the rest. Key properties of priority que
    3 min read
  • Python | sympy.as_ordered_terms() method
    With the help of sympy.as_ordered_terms() method, we can get the terms ordered by variables by using sympy.as_ordered_terms() method. Syntax : sympy.as_ordered_terms() Return : Return ordered terms in mathematical expression. Example #1 : In this example we can see that by using sympy.as_ordered_ter
    1 min read
  • Python | Inverse Sorting String
    Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String
    3 min read
  • Python reversed() Method
    reversed() function in Python lets us go through a sequence like a list, tuple or string in reverse order without making a new copy. Instead of storing the reversed sequence, it gives us an iterator that yields elements one by one, saving memory. Example: [GFGTABS] Python a = ["nano",
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences