DEV Community

Evans Van
Evans Van

Posted on

Introduction to Data Structures and Algorithms.

Introduction

A data structure is a particular way of organizing data in a computer so that it can be used effectively. Data Structures allows you to organize your data in such a way that enables you to store collections of data, relate them and perform operations on them accordingly. Python features both built-in data structures such as lists, sets, dictionaries and tuples and user defined such as linked lists, trees and graphs.

Built-in Data Structures

These as the name suggest come out of the box in python making them easy to use and programming easier.Lets break them down better

Lists

These are used to store data of the same type or different types in a sequential manner. They are indexed starting from 0 and the ending index is N-1 (If N elements are there). It also has negative indexing starting from -1 which allows you to access elements from last to first.
List are created using square brackets if you don't pass any elements then you get an empty list.

List Manipulation and Functions

List items can be accessed using the index values other list can also be modified using

  1. append() adding all elements passed o it as a single element.
  2. Insert() takes an element and index, adds element to the index value and increases list size.
  3. Del which is a python keyword deletes element at index provided
  4. pop() function removes element from the indexed value.
  5. remove() this is used to remove an element using its value.
  6. len() returns length of the list.
  7. count() this function finds the count of the value passed to it.
  8. sort() and sorted() these functions do the same thing by sorting he list values. The sorted() has a return where as sort() modifies original list.

Examples

test = [1,2,'one','two']
test[0] # returns value at 0 index which is 1
test[-1] # returns the last value in the list 'two'

test.append(3) #adds 3 to end of list returns [1,2,'one','two',3]

test.insert(1,3) # adds element to index provided returns [1,3,2,'one','two']

del test[2] # deletes element at index 2 return [1,2,'two']

test.pop(1) #pops the element at provided index returns [1,'one','two']

test.remove('two') # removes the provided element returns [1,2,'one']

len(test) # returns length of list which is 3 here

test.count(2) # finds count of value passed which is 1 here
Enter fullscreen mode Exit fullscreen mode

Dictionaries

These are used to store key-value pairs the data can be the same or of different types. Dictionaries are created using curly braces {} or using the dict() function.

Dictionary Manipulation and Functions
  1. To change dictionary values you have to use the keys. So you firstly access the key and then change the value accordingly.
  2. To add values you simply just add another key-pair value.
  3. To delete items you use the pop function.
  4. To clear the dictionary you use the clear() function.
  5. To access elements in the dictionary you use the key values.
  6. You can also access the keys, values or all items of a dictionary using the key(), value() and items() functions.

Examples

mydict= {} #initializes empty dict
mydict= {1:'one', 2:'two'} # dict with two elements
mydict[1] # returns (one)
mydict[3]= 'three' # {1:'one', 2:'two', 3:'three'}

mydict.pop(2) # returns {1:'one'}
mydict.clear() # returns an empty dict {}

mydict.keys() # gets keys returns  # (1,2)
mydict.values() # gets values returns  # (one,two)
mydict.items() # returns {1:'one', 2:'two'}
Enter fullscreen mode Exit fullscreen mode

Sets

Sets are an unordered collection of unique elements meaning that even if data is repeated more than once the set will only keep one value. Sets are created using the curly braces and just passing values.

Set Manipulation and Functions
  1. add() this is used to add elements where you just pass the value you want added.
  2. union() this is used to combine data in the sets.

Examples

myset = {1.2.3,1} #returns {1,2,3} no duplicates allowed
myset.add(3) # returns {1,2,3,4}
Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are similar to list but are immutable this means that once created they cannot be modified. Just like a list ttuples can contain data of various types. Tuples are created using parenthesis or using the tuple() function.

Tuple Manipulation and Functions
  1. Accessing items is done the same way as for lists.
  2. To add items you use the + which takes another tuple to be appended to it.

Examples

mytup = (1.2.3) 
mytup[1] # returns (2)
Enter fullscreen mode Exit fullscreen mode

Conclusion

This concludes the deep dive into the built-in methods for python.These are the primary methods used by most python programmers to create and manage their data, they also have a variety of functions apart from the ones i have described here used to manipulate data. In the next article we will deep dive into the user defined methods.

Top comments (0)