Data structures in python

Data is very important in all aspects, the way data is stored and organized, so that we can use it in a meaningful way is known as data structures.

In that note python also has very interesting data structures in it, here is the primary summary list,

  1. List
  2. Dict
  3. Set
  4. Tuple

List

List is more likely an array or a sequence of logical elements grouped together.

Elements enclosed with [ ] brackets are known as lists in python.

l = [1, 2, 3, 4, 5]
print(l)

type will return the type of the variable. Now the variable l denotes that its a list of numbers from 1 to 5.

Each element in the list can be acceseed by its index. The index starts from 0 and not from 1

This is a very basic implementation of list, we can do much more.

Dict

Dict is basically a key, value pair. We can assume, key as the variable name holding its value.

Its values are enclosed with { } brackets.


Each value in the dict can be acceseed by its key.

This is a very basic implementation of dict, we can do much more.

Set

It is as same as list, logically grouping of elements. But the prime difference is that unlike list

  1. It doesn’t allow duplicates (Line #6 & #9)
  2. It will be ordered list of elements (Line #9)
  3. It doesn’t support indexing (Line #11)

Its values are enclosed with { } brackets.

The only way of accessing elements is through iteration. Hold on does the iteration give exact order of result expected? (Line #18)

Tuple

Tuple is same as list, logical group of elements. The only difference is that tuples are immutable where a list is mutable.

Its values are enclosed with ( ) brackets

These are base data structures used in python. With these we will be able to implement much more complex data structures like linked list, Queue.

Leave a reply:

Your email address will not be published.