PYTHON DATA STRUCTURES — Lists, Tuples, Sets, Dictionaries

Queen
3 min readMar 28, 2023

--

The Python data structures can confuse or overwhelm a beginner in the data science path. This article helps to understand the difference between lists, tuples, sets, and dictionaries, the similarities, and applications.

  • Explanation of Data Structures and how it works.
  • Lists
  • Tuples
  • Sets
  • Dictionaries
  • Conclusion
  • Reference

DATA STRUCTURES

While the data structures used in Python are more than the 4 mentioned above. These 4 types are the most commonly used to store and organize data.

Before we discuss these data structures. Let me explain the concept of data structures to you. Do you know how you can’t put eggs in a polythene bag? After all, it wouldn’t be safe, or easy to arrange, most especially because it’s not the most appropriate carriage assigned to eggs.

Another example is how arranging a book horizontally into a bag that should vertically house the book would be chaotic.

Now, let’s explain these generic examples technically in relation to data.

Data structure gives us easier access to data. For easy access, there would be efficient organization and storage. (To eat an egg, you have to know the container you stored it in and know how to access it and what to do with it, and how to do it. Cook? Fry? Boil?)

Data Structure is the proper arrangement or organization of data, including storage.

Different data structures include Linked- List, Stacks, Queue, Graph, etc. However, the built-in data structures in Python are — Lists, tuples, sets, and dictionaries.

The application of lists is different from the application of sets, and the same explanation goes for tuples and dictionaries.

These built — in data structures have different qualities, properties, and applications. They are applied considering the datasets the programmer uses, how the program gets executed, and the program’s end goal.

Please note that technical terms that you come across are not entirely different from the grammatical meaning you know. They are just interpreted technically.

LISTS

  • Identified and represented by the python objects in square brackets — []
  • Lists can store elements of mixed data types
  • Elements in lists can be duplicated.
  • The data in a list are mutable — can be modified or/and changed.
  • Sequential and ordered — The elements maintain the order in which they were input.
  • Supports indexing and slicing.
  • Below are examples of the methods used in a list: append(), remove(), sort(), clear(), pop(), index()

TUPLES

  • Identified and represented by the objects in parenthesis — ()
  • Tuples can store elements of different types
  • Elements in tuples can be duplicated.
  • The data in tuples are immutable — elements can not be added or removed.
  • Sequential and ordered.
  • Supports indexing and slicing.
  • Below are examples of the methods used: tuple(), count(), index()

SETS

  • Represented by curly brackets — {}
  • Sets can store elements of different data types, such as strings, data, and booleans, but cannot store mutable elements like lists, or dictionaries
  • Elements in sets are unique.
  • The data in sets are mutable — objects can be added or removed, but cannot be changed.
  • Non—ordered.
  • Does not support indexing or slicing.
  • Below are examples of the methods used: len(), discard(), intersection(), add(), clear(), copy(), union()

DICTIONARIES

  • Represented by curly brackets — {}
  • While ‘key’ must be of an immutable data type, such as strings, or tuples, ‘value’ can hold every data type.
  • While keys are unique, values can be duplicated, that is, different keys can have the same value.
  • The data in dictionaries are mutable.
  • Ordered.
  • Unordered in Py 3.6 and older.
  • The key is used to index.
  • Below are examples of the methods used: get(), copy(), fromkeys(), pop(), values()

Conclusion

Overall, this article provides a beginner with a foundational knowledge of python data structures and their respective applications. Hence, leveraging the advantages of these data structures to code more efficiently.

Reference

https://docs.python.org/3.11/tutorial/datastructures.html

--

--