DEV Community

Cover image for PYTHON DATA STRUCTURES (LISTS, SETS, TUPLES, DICTIONARIES)
Maame Afia Fordjour
Maame Afia Fordjour

Posted on

2 1 1 1 1

PYTHON DATA STRUCTURES (LISTS, SETS, TUPLES, DICTIONARIES)

INTRODUCTION

Depending on the circumstance, data can be organized using data structures to facilitate more efficient access. Any programming language's foundational concepts, or data structures, are what a program is constructed upon. Compared to other programming languages, Python makes it easier to master the fundamentals of these data structures.

We'll talk about Python data structures in this article, along with their relationships to particular Python data types. We will go over all of the built-in data structures, including dictionaries and list tuples.

LISTS

Python lists are an ordered collection of data, just like arrays, which are stated in other languages. Since items in a list do not have to be of the same type, it is immensely flexible.

Python lists are implemented similarly to Java's ArrayList and C++'s Vectors. Since every piece needs to be moved, the most expensive action is adding or removing the element from the beginning of the list. When the preallocated memory runs out, insertions and deletions at the end of the list may also become expensive.

As demonstrated below, we can make a list in Python.

đź“ŚExample:

pylist= [1, 2,  3, "apple", 2.3]
print(pylist)
Enter fullscreen mode Exit fullscreen mode

đź“Ś The allocated index can be used to access list elements. Python lists have a starting index of 0 and an ending index of N-1, if there are N elements in the list.

DICTIONARY

Python dictionaries have an O(1) time complexity, just like hash tables in other programming languages. A dictionary, as opposed to other data types that only include a single value as an element, contains a key:value pair. It is an unordered collection of data values that is used to store data values similar to a map. To improve optimization, the dictionary provides key-value.

With the use of keys, the Python Dictionary is indexable. These can be of any hashable type, meaning they are objects such as strings, numbers, tuples, etc. that are immutable. Curly braces (}) or dictionary comprehension can be used to construct a dictionary.

đź“ŚExample:

Dict = {'Name': 'John Doe', 1: [1, 2, 3, 4]}
print("Creating Dictionary: ")
print(Dict)

Enter fullscreen mode Exit fullscreen mode

TUPLE

Similar to a list, a Python tuple is a collection of Python objects; however, once a tuple is generated, its elements cannot be added or withdrawn. A Tuple can have different kinds of elements, much like a List.

Tuples in Python are constructed by grouping a sequence of values together using parentheses or not, and then separating the values with a "comma."

Note: Although it is a little more difficult, tuples can also be made with just one element. To make something a tuple, there needs to be a trailing "comma" in addition to the one element enclosed in parentheses.

đź“ŚExample:

Tuple = ('apple', 'orange')
print("\nTuple with the use of String: ")
print(Tuple)

Enter fullscreen mode Exit fullscreen mode

SET

Python Sets are changeable, unordered data collections that forbid duplicate elements. In essence, sets are utilized for duplicate entry removal and membership checking. This uses a data structure called hashing, which is widely used to execute insertion, deletion, and traversal operations in O(1) on average.

A linked list is created by appending a value to an index point if there are several entries present at that index location. A dictionary containing dummy variables is used to implement CPython sets, with the members set having the highest optimizations for time complexity being the key.

đź“ŚExample:

Set = set([1, 2, 'Apple', 4, 'orange', 6,]) 
print("\nSet with the use of Mixed Values") 
print(Set) 
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (3)

Collapse
 
samir419 profile image
Samir •

Very helpful

Collapse
 
skipperhoa profile image
Hòa Nguyễn Coder •

Thanks you ! You can try use python write crawl web ::)

Collapse
 
maame-codes profile image
Maame Afia Fordjour •

Will do! Thanks :)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay