Update: Data Classes

With the introduction of Data Classes in Python 3.7 we get very close.

The following example is similar to the NamedTuple example below, but the resulting object is mutable and it allows for default values.

from dataclasses import dataclass


@dataclass
class Point:
    x: float
    y: float
    z: float = 0.0


p = Point(1.5, 2.5)

print(p)  # Point(x=1.5, y=2.5, z=0.0)

This plays nicely with the new typing module in case you want to use more specific type annotations.

I've been waiting desperately for this! If you ask me, Data Classes and the new NamedTuple declaration, combined with the typing module are a godsend!

Improved NamedTuple declaration

Since Python 3.6 it became quite simple and beautiful (IMHO), as long as you can live with immutability.

A new way of declaring NamedTuples was introduced, which allows for type annotations as well:

from typing import NamedTuple


class User(NamedTuple):
    name: str


class MyStruct(NamedTuple):
    foo: str
    bar: int
    baz: list
    qux: User


my_item = MyStruct('foo', 0, ['baz'], User('peter'))

print(my_item) # MyStruct(foo='foo', bar=0, baz=['baz'], qux=User(name='peter'))
Answer from Rotareti on Stack Overflow
Top answer
1 of 16
629

Update: Data Classes

With the introduction of Data Classes in Python 3.7 we get very close.

The following example is similar to the NamedTuple example below, but the resulting object is mutable and it allows for default values.

from dataclasses import dataclass


@dataclass
class Point:
    x: float
    y: float
    z: float = 0.0


p = Point(1.5, 2.5)

print(p)  # Point(x=1.5, y=2.5, z=0.0)

This plays nicely with the new typing module in case you want to use more specific type annotations.

I've been waiting desperately for this! If you ask me, Data Classes and the new NamedTuple declaration, combined with the typing module are a godsend!

Improved NamedTuple declaration

Since Python 3.6 it became quite simple and beautiful (IMHO), as long as you can live with immutability.

A new way of declaring NamedTuples was introduced, which allows for type annotations as well:

from typing import NamedTuple


class User(NamedTuple):
    name: str


class MyStruct(NamedTuple):
    foo: str
    bar: int
    baz: list
    qux: User


my_item = MyStruct('foo', 0, ['baz'], User('peter'))

print(my_item) # MyStruct(foo='foo', bar=0, baz=['baz'], qux=User(name='peter'))
2 of 16
388

Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's named tuple recipe if you need to support Python 2.4.

It's nice for your basic example, but also covers a bunch of edge cases you might run into later as well. Your fragment above would be written as:

from collections import namedtuple
MyStruct = namedtuple("MyStruct", "field1 field2 field3")

The newly created type can be used like this:

m = MyStruct("foo", "bar", "baz")

You can also use named arguments:

m = MyStruct(field1="foo", field2="bar", field3="baz")
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › writing › structure
Structuring Your Project — The Hitchhiker's Guide to Python
Ravioli code is more likely in Python: it consists of hundreds of similar little pieces of logic, often classes or objects, without proper structure. If you never can remember, if you have to use FurnitureTable, AssetTable or Table, or even TableNew for your task at hand, then you might be swimming in ravioli code. Python modules are one of the main abstraction layers available and probably the most natural one. Abstraction layers allow separating code into parts holding related data and functionality. For example, a layer of a project can handle interfacing with user actions, while another would handle low-level manipulation of data.
Discussions

What is struct in Python terms?
Both struct and class in C++ define a user-defined type. They only differ by implicit inheritance and access to members (public and private). More on reddit.com
🌐 r/cpp_questions
14
6
May 14, 2020
Python Classes vs Functions. How should I structure my code?
There's no best way and unless you're doing something absolutely wild, the memory of having an object is irrelevant. So your senior's technical reasoning is wrong. Nevertheless... Here's some professional advice: do what your senior tells you to. If he doesn't understand how classes work then it's likely most of the team doesn't. The best code is code your team understands. My python advice: personally, I'd say classes are great if you want to have an object that contains other variables and functions to manipulate them. They're especially good if you want to make multiple objects from the same template. If your classes are just collections of functions that you're keeping together for organization's sake, that's not a class it's a module. There's no benefit to the class structure in that case and an average python programmer will expect a module so just use a module. He's definitely correct about the files by the way. Putting just one module/class/program in one file is typically good organization. More on reddit.com
🌐 r/learnpython
22
9
December 22, 2022
What is the optimal structure for a Python project?
3 tips from me: When you use typing everywhere then you are forcing yourself to omit the circular dependencies (we cannot have circular dependencies imports in python) between modules which is the easiest way to keep a very basic structure. Think of the modules hierarchy while structuring the python code. Which module is high level (e.g. dashboard, GUI) and which is low level (e.g. some utils module build upon a standard library). Always import a module that has a lower level of abstraction (or in other words, lower complexity) in the module with higher complexity. E.g. GUI module imports storage access module and not the other way around. More on reddit.com
🌐 r/Python
52
253
December 25, 2023
Can someone tell me what are the best resources to master data structures and algorithms in Python.
Simplified intro to algorithms: Grokking Algorithms . Advanced algorithms and data structures: Data Structures and Algorithms in Python . The second one is written by a bunch of doctors. I feel like their coding style is kind of too C/Java-esque and not entirely pythonic at times, but the DSA part is really good. More on reddit.com
🌐 r/learnpython
53
312
January 25, 2022
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-data-structures
Python Data Structures - GeeksforGeeks
Python Set is an unordered collection of data that is mutable and does not allow any duplicate element. Sets are basically used to include membership testing and eliminating duplicate entries. The data structure used in this is Hashing, a popular technique to perform insertion, deletion, and traversal in O(1) on average.
Published   July 23, 2025
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
1 month ago - Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...
🌐
Real Python
realpython.com › python-data-structures
Common Python Data Structures (Guide) – Real Python
October 21, 2023 - For example, a motor home parking lot wouldn’t allow bikes to be parked on it. A restricted parking lot corresponds to a typed array data structure that allows only elements that have the same data type stored in them. Performance-wise, it’s very fast to look up an element contained in an array given the element’s index. A proper array implementation guarantees a constant O(1) access time for this case. Python includes several array-like data structures in its standard library that each have slightly different characteristics.
🌐
Vagrant
ashki23.github.io › python-str.html
Python data structures
Let assume we have two variables a = 10 and b = 20 and want a = b = 20 and b = a + b = 30. For example: a = 10 b = 20 a = b b = a + b (a,b) ## (20, 40) # we wanted (20, 30) ## Using tuple a = 10 b = 20 a, b = (b, a + b) # or a, b = b, a + b (a,b) ## (20, 30) Dictionaries (also called dicts) ...
Find elsewhere
🌐
Dataquest
dataquest.io › blog › data-structures-in-python
Python Data Structures: Lists, Dictionaries, Sets, Tuples – Dataquest
May 12, 2025 - Dictionaries in Python are very similar to real-world dictionaries. These are mutable data structures that contain a collection of keys and, associated with them, values. This structure makes them very similar to word-definition dictionaries. For example, the word dictionary (our key) is associated with its definition (value) in Oxford online dictionary:
🌐
Medium
medium.com › swlh › structures-in-python-ed199411b3e1
Structures in Python. In Python, there is a way where you can… | by Akshay Chavan | The Startup | Medium
September 14, 2020 - In Python, there is a way where you can define a collection that mimics properties similar to the structure by using namedtuple under collections. An example below, a collections.namedtuple Point is defined that contains x and y fields.
🌐
Edureka
edureka.co › blog › data-structures-in-python
Data Structures in Python | List, Tuple, Dict, Sets, Stack, Queue
November 27, 2024 - This example covers the creation, modification, and addition of elements to a bytearray. ... The collections module in Python provides specialized container data types that extend the functionality of built-in types like lists, dictionaries, and tuples. Here are some key data structures from the ...
🌐
Built In
builtin.com › data-science › python-data-structures
What Are Python Data Structures? (Definition, Examples) | Built In
June 23, 2025 - A user-defined data structure can ... modules. In Python, user-defined data structures can include arrays, stacks, queues, trees, linked lists, graphs and hash maps....
🌐
Swaroopch
python.swaroopch.com › data_structures.html
Data Structures - A Byte of Python - SwaroopCH.com
A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. This is easy to imagine if you can think of a shopping list where you have a list of items to buy, except that you probably have each item on a separate line in your shopping list whereas in Python you put commas in between them.
🌐
Corporate Finance Institute
corporatefinanceinstitute.com › home › resources › python data structures
Python Data Structures - Overview, Types, Examples
November 23, 2023 - A list is defined as an ordered collection of items, and it is one of the essential data structures when using Python to create a project. The term “ordered collections” means that each item in a list comes with an order that uniquely identifies them.
🌐
ScholarHat
scholarhat.com › home
Data Structures in Python - Types & Examples(A Complete Guide)
September 10, 2025 - Explore the essential data structures in Python with types and examples. Learn lists, tuples, dictionaries, stacks, queues, and more in this complete guide for developers.
🌐
Mimo
mimo.org › glossary › python › data-structures
Python Data Structures: Syntax, Usage, and Examples
They help programmers manage collections of data and perform operations such as searching, sorting, and modifying data. Python provides built-in data structures like lists, tuples, sets, and dictionaries, as well as more advanced structures like trees, graphs, queues, linked lists, and hash tables.
🌐
Kitchin Research Group
kitchingroup.cheme.cmu.edu › blog › 2013 › 02 › 27 › Some-basic-data-structures-in-python
Some basic data structures in python
February 27, 2013 - We often have a need to organize data into structures when solving problems. A list in python is data separated by commas in square brackets.
🌐
Medium
medium.com › @techwithpraisejames › a-beginners-guide-to-mastering-python-data-structures-with-practical-examples-95befa4fb367
A Beginner’s Guide to Mastering Python Data Structures(With Practical Examples)
April 11, 2025 - ... Data structures are like warehouses where you can store and organize data in different ways. There are four built-in data structures in Python programming language namely: lists, tuples, dictionaries, and sets.
🌐
ENV 859
env859.github.io › python › data-structures.html
Python 101 - Python Data Structures - ENV 859
The key to mastering the use of ... include: Determining unique features, properties, and methods associated with each data structure object; Determining the syntax for creating each type of data structure object; Understanding how specific elements are identified, added, removed, etc from the data structure; Similar to our previous session, we again lean on VanderPlas’ · Whirlwind Tour of Python materials for detailed descriptions of these objects and examples of their ...
🌐
DataCamp
datacamp.com › tutorial › data-structures-python
Python Data Structures with Primitive & Non-Primitive Examples | DataCamp
April 6, 2023 - This is when a dictionary can come in handy. Dictionaries are made up of key-value pairs. key is used to identify the item and the value holds as the name suggests, the value of the item. x_dict = {'Edward':1, 'Jorge':2, 'Prem':3, 'Joe':4} del x_dict['Joe'] x_dict ...