fbpx

Category content:

Data Structure in Python

Data Structure in Python

Data structure in python makes it possible to store and organize data in a very efficient way. Using it, we can link the data together and perform the desired operations on them.
In programming, this process is especially important because it enables us to access and recall data more easily.

There is four general Data Structures in Python:

  • List: ordered, changeable, duplicate members.
  • Dictionary: ordered, changeable, No duplicate members.
  • Set: unordered, unchangeable, unindexed, No duplicate members.
  • Tuples: ordered, unchangeable, duplicate members.

LIST: 

>>>a = []

for example:

>>>a = [2, 5, 10]             >>>b = [1.2, 5.9, “Hi”, 8]

To recall form List: >>>a [0]      result:  2              >>>a [1]      result:  5

To change in List : >>>a [0]=3           >>>a          result  a[3, 5, 10]

Methods :
  • append(): Add element end of list
  • remove(): Remove first element
  • clear(): Remove all list
  • copy(): Copy of list
  • count(): Number of elements with value
  • extend(): Add to end of list
  • index(): Index of first element with value
  • insert(): Add element to the list
  • pop(): Remove element from list
  • reverse(): Reverse list
  • sort(): Sort list

for i in a:

print(i)

Result: 3   5   10

let’s to see video from our YouTube channel

DICTIONARY: 

Different between Dictionary with other Data Structure is, we can make Data with TAG, now you ask what is mean? let’s to see some examples:

>>>a = {}             >>>a = {“Quantiy”: 5, “Price”: 20}

>>>b = {}             >>>b = {0: 5, 1:20}

In the second example, we use Tag 0 for “Quantity” and Tag 1 for “Price”, that’s same as first one.

>>>c = {}          >>>c = {“name”: “XXX”, “family”: “YYY”}

for z,x in c.items():

print(z, ” : “, x)

Result

name : XXX
family : YYY

Methods:
  • clear(): Removes all
  • copy(): get copy
  • fromkeys(): get the specified keys and value
  • get(): get value of the specified key
  • items(): get a list for each key value pair
  • keys(): get a list with keys
  • pop(): Removes by specified key
  • popitem(): Removes the last element
  • setdefault(): get the value of the specified key
  • update(): Updates with the specified key-value pairs
  • values(): get a list of all the values

in this structure, we can make for each element, one Tag, so that’s too easy to save and recall data of this method.

let’s to see video from our YouTube channel

SET: 

SET structure is same as DICTIONARY but in SET we dont have TAG. let’s to see some examples:

>>> a = {}         a = {1, 2, 3}

in this structure if we have same number, it will remove by compiler, that’s mean, we dont have repeat in this structure.

>>> a = {1, 4, 2, 5, 1, 6, 2} ==> compiler remove 1, 2 because we have same.

for i in a:

print(i)

>>1, 2, 3, 5, 6

Methods :
  • add(): Adds element
  • clear(): Removes all
  • copy(): get a copy
  • difference(): get difference between two or more sets
  • difference_update(): Removes the items included in another
  • discard(): Remove the item
  • intersection(): get the intersection of two more sets
  • intersection_update(): Removes the items that do not present in other
  • isdisjoint(): get a intersection or not
  • issubset(): get contains or not
  • issuperset(); get contains another set or not
  • pop(): Removes element
  • remove(): Removes the specified element
  • symmetric_difference(): Get symmetric differences of two sets
  • symmetric_difference_update(): inserts the symmetric differences
  • union(): get containing the union of sets
  • update(): Update

let’s to see video from our YouTube channel

TUPLE: 

TUPLE is unchangeable, and when we assign value, we cannot change it after.

in TUPLE, we have access to the element by command for example a = [0], in this command it return value of first element of these data

>>>a = ()                 >>>a = (1, 2, 3, 4)     or           >>> a = 1, 2, 3, 4

as you see TUPLE is same as int, if we want to make TUPLE with one element, we must use comma after it, let’s see

>>> a = (1,)     or     >>>a = 1,

Methods :
  • count(): Get the number of times with specified value
  • index(): Get the position of where it was found

let’s to see video from our YouTube channel

If you want to learn more Python, please click here.

Share :
350
keyboard_arrow_up