Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Faith Mueni Kilonzi
Faith Mueni Kilonzi

Posted on

     

Data Structures in Python

DATA STRUCTURES IN PYTHON
You can never be an efficient programmer without mastering data structures and algorithms. Problem solving in programming is impossible without the basics. Regardless of which aspect of python programming you want to focus on, you can’t ignore data, its organization and the algorithms you need to find solutions to their manipulation and data analysis.
Data structures are a way of organizing data in such a way that you can store collections of data, relate the, and perform operations on them. Data structures are derived from the term: data abstraction which is the use of data structures to solve problems by focusing on the main picture without getting lost in the details of meta-data and data access. They ease the process of problem-solving and ensure efficiency.
In other words, data structures are an actual implementation of the Abstract Data Types (ADT). This implementation requires you to have a physical view (how data collections are stored and organized) in a particular programming language’s constructs and basic data types.
There are two ways of grouping data structures in python:
(i) Built-in and User-Defined Data Structures

Data Structures in Python

(ii) Primitive and Non-Primitive Data Structures
Primitive and Non-Primitive Data Structures

Primitive Data Structures

  1. Integers : Used to represent numeric data, more specifically whole numbers from negative infinity to infinity, like 2,3, -100000, 10000.
  2. Float : ‘Float’ stands for floating point number. It is used for rational numbers usually ending with decimal figure such as 1.1,2.3,9.3 etc.
  3. String : Strings are a collection of alphabets, words or other characters. In Python, strings are created by enclosing a sequence of characters within a pair of double or single quotes. x= “Hello World”.Explore the different string operations here

  4. Boolean : It is a built-in data type that can take the values TRUE or FALSE

Non-Primitive Data Structures

This section covers some of the primitive (and built-in) Python Data Structures.

  1. Lists
    Stores indexed elements that are changeable and can contain duplicate items.
    Mutable, ordered series, traditionally of the same type of object.
    Advantages: Mutable and ordered. Easy to understand. Relatively efficient memory usage.
    Disadvantages: Searching is O(n).
    To create a list, use square brackets:
    mylist = [ ]
    mylist = [1,2,3]
    mylist = ['a', 'b', 'c', [1,2,3] ] # 4 elements

  2. Tuples
    Stores indexed, unchangeable elements that can have duplicate copies.
    Immutable, ordered series traditionally containing different objects
    Advantages: Immutable and ordered. Relatively efficient memory usage (more than lists).
    Disadvantages: Searching is O(n). Hard to understand for many Python newcomers.

  3. Dictionaries
    Mutable, unordered pairs (keys and values) of objects. Keys must be hashable.
    Advantages: O(1) searching for keys. Makes it easy to create trees and other hierarchical data structures. Can be used to create self-documenting code. Many problems can be described in terms of key-value pairs.
    Disadvantages: Only lookup by key. Uses more memory than lists and tuples. Keys must be hashable.

  4. Sets
    A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
    Once a set is created, you cannot change its items, but you can add new items.
    Advantages:Relatively efficient memory usage (more than lists).
    Disadvantages: Sets are unordered, so you cannot be sure in which order the items will appear.Searching is O(n).

We will cover each of the data structures in the next tutorials. Thank you for reading!

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
safinghoghabori profile image
Safin Ghoghabori
Full-stack dev
  • Location
    India
  • Education
    MCA
  • Joined

What is the difference between list and tuple? In same problem we can use either both of them?

CollapseExpand
 
fronkan profile image
Fredrik Sjöstrand
Software engineering. Interested in data science, machine learning, and automation. I also have a crush on python.
  • Location
    Linköping, Sweden
  • Education
    Master of Science in Computer Science and Engineering, specialized in ML and AI
  • Joined
• Edited on• Edited

Tuples are immutable while lists are mutable. This for example has the effect that tuples are hashable and therefore, can be used in sets and as keys in dictionaries.
Also, this makes lists the better option when you need to addi or remove items. If you doesn't, as mentioned in the article, the tuples has lower memory footprint.

CollapseExpand
 
safinghoghabori profile image
Safin Ghoghabori
Full-stack dev
  • Location
    India
  • Education
    MCA
  • Joined

Ohk thanks for your warm reply

CollapseExpand
 
kingtomi profile image
Ayodabo Oluwatomisin
I am a python software developer, mainly Data Science and Backend Development using Django. I love everything Python
  • Location
    University of Ibadan, Ibadan Nigeria
  • Education
    Computer Science
  • Work
    Student
  • Joined

List are mutable non primitive data Structures while tuple is the opposite, immutable. You make use of list when you will be modifying the contents from time to time, you use tuples when you don't need to modify it after creating it. It places a restriction and helps makes computation faster.

CollapseExpand
 
global_codess profile image
Faith Mueni Kilonzi
I am a full-stack software engineer, technical writer, and DevOps enthusiast, with a passion for problem-solving through implementation of high-quality software products.I hold a bachelor’s degree
  • Email
  • Location
    Nairobi, Kenya
  • Education
    BSc. Computer Science, Ashesi University
  • Work
    Software Engineer and Technical Writer
  • Joined

Nice explanation.

CollapseExpand
 
msnmongare profile image
Sospeter Mong'are
Software Engineer passionate about developing for the web
  • Location
    Kenya
  • Education
    BSC. Software Engineering
  • Pronouns
    Mr
  • Work
    Software Engineer (Backend) | API Development & Integration | Technical writer | Mentor
  • Joined

Thank you Faith for sharing this🙏🙏

CollapseExpand
 
ianakotey profile image
ianakotey
Not your average geek. An aspiring software developer, python is by far my fav
  • Location
    Ghana
  • Work
    Mr at Student
  • Joined

Just a little clarification. Sets are mutable and unordered

CollapseExpand
 
global_codess profile image
Faith Mueni Kilonzi
I am a full-stack software engineer, technical writer, and DevOps enthusiast, with a passion for problem-solving through implementation of high-quality software products.I hold a bachelor’s degree
  • Email
  • Location
    Nairobi, Kenya
  • Education
    BSc. Computer Science, Ashesi University
  • Work
    Software Engineer and Technical Writer
  • Joined

Thank you for the clarification. I made the change.

CollapseExpand
 
vincenttommi profile image
Vincent Tommi
python Developer
  • Email
  • Location
    Nairobi-Kenya
  • Education
    Moringa-School
  • Pronouns
    Thinking Beyond Code
  • Work
    Freelance
  • Joined

educative alot__

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a full-stack software engineer, technical writer, and DevOps enthusiast, with a passion for problem-solving through implementation of high-quality software products.I hold a bachelor’s degree
  • Location
    Nairobi, Kenya
  • Education
    BSc. Computer Science, Ashesi University
  • Work
    Software Engineer and Technical Writer
  • Joined

More fromFaith Mueni Kilonzi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp