0% found this document useful (0 votes)
181 views10 pages

Data Structures in Python

Uploaded by

Manu Manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views10 pages

Data Structures in Python

Uploaded by

Manu Manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATA STRUCTURES IN PYTHON:

 Data Structures – Data Structure is a process of manipulates the data.


Manipulate is a process of organizing, managing and storing the data in
different ways.
 It is also easier to get the data and modify the data. The data structure
allows us to store the data, modify the data and also allows to compare the
data to others. Also, it allows performing some operations based on data.

TYPES OF DATA STRUCTURES:


 BUILT–IN DATA TYPES
 USER DEFINED DATA TYPES

BUILT-IN DATA TYPES


Python having some implicit data structure concepts to access and store the data.
The following are the implicit or Built-in Data structures in python.
1. List
2. Tuple
3. Dict
4. Set
LIST :
 Lists are used to store multiple items in a single variable.
 A single list may contain DataTypes like Integers, Strings, as well as Objects.
Lists are mutable.
 The list is also a sequence data type which is used to store the collection
of data.
 List can allow duplicate values.
CREATING A LIST:
Creating a list using square brackets as well list constructor also , list also
be empty list because it is mutable.
Sample code -
my_l1=[] Output:
print(my_l1) []
[1, 2, 3, 4, 5, 'civil']
list2=[1,2,3,4,5,'civil']
[1, 2, 3]
print(list2)
l3=list([1,2,3])
print(l3)
LIST METHODS:
Function Description

APPEND() Add an element to the end of the list

EXTEND() Add all elements of a list to another list

INSERT() Insert an item at the defined index

REMOVE() Removes an item from the list

CLEAR() Removes all items from the list

INDEX() Returns the index of the first matched item

COUNT() Returns the count of the number of items passed as an argument

SORT() Sort items in a list in ascending order

REVERSE() Reverse the order of items in the list

COPY() Returns a copy of the list


Function Description

It will remove the last element of the list straight away. If you pass index
POP()
as a parameter then, it removes the element in the index.

Sample code:
For Adding the elements - append(), insert(), extend()

l1=list([1,2,3]) Output:
print(l1) [1, 2, 3]
l1.append(10) #append()
print(l1) [1, 2, 3, 10]
l1.insert(3,55) #insert()
print(l1) [1, 2, 3, 55, 10]
l2=[40,20]
l1.extend(l2) #extend()
[1, 2, 3, 55, 10, 40, 20]
print(l1)

Sample code:
For Deleting the elements - del(), pop(), remove(), clear()

l1=[1,2,3,4,5,6,7]
print(l1)
Output:
del l1[3] #del()
[1, 2, 3, 4, 5, 6, 7] # l1
print(l1)
[1, 2, 3, 5, 6, 7] # after del
l1.remove(3) #remove()
[1, 2, 5, 6, 7] #after remove
print(l1)
[1, 2, 5, 6] #after pop
l1.pop() #pop()
[] #after clear
print(l1)
l1.clear() #clear()
print(l1)
Accessing the Elements:
Elements are accessed in the list is based on the index value. Also, we use some
slicing concepts to access values in the list.
Example:
list=[1,2,3,4,5]
print(list[3]) # it will return 3 indexed element as 4.
print(list[:]) # it will return all the elements in a list
print(list[1:]) # it will return the elements from index 1.
print(list[:3]) # it will return the elements upto index 3.
print(list[-1]) # it will return the last element.
print(list[:-1]) # it will return the elements upto -1 i.e last element.

OUTPUT:
4
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[1, 2, 3]
[5]
[1, 2, 3, 4]

OTHER METHODS
Sample code - For Index(), count(), copy(), len() , reverse(), sort()

l1=[1,2,8,4,5,6,6,6] OUTPUT:
print(l1.index(4)) 2 #index
print(l1.count(6)) 3 #count
l2=l1.copy() [1, 2, 8, 4, 5, 6, 6, 6] #copy
print(l2) 8 #len
print(len(l2)) [6, 6, 6, 5, 4, 8, 2, 1] # reverse
l2.reverse() [1, 2, 4, 5, 6, 6, 6, 8] #sort
print(l2)
l1.sort()
print(l1)
TUPLES:
 Tuples are used to store multiple items in a single variable and allows
duplicate members.

 A tuple is a collection which is ordered and unchangeable.

EXAMPLE : CREATING A TUPLE :

tuple = ("apple", "banana", "cherry")


print(tuple)

output:
('apple', 'banana', 'cherry')

ACCESSING THE ELEMENTS :

my_t=(1,2,3,4) Output:
print(my_t) (1, 2, 3, 4)
print(my_t[2]) 3
print(my_t[:]) (1, 2, 3, 4)

Appending Elements:
Adding the elements to a tuple is done using the '+' symbol. It just concatenates
the tuples of values.
Example:
my_t=(1,2,3,4)
my_t1=(7,8,9)
my_t=my_t+my_t1
print(my_t)

Output:
(1,2,3,4,7,8,9)

Other Functions:
Tuples are not allowing any manipulation methods because it is considered as an
immutable collection of values. So it using only the following methods.
 count() - It returns the number of occurrences of a particular element.
 index() - It returns the position of the particular element.

Example:
my_t=(1,2,3,2)
print(my_t)
print(my_t.count(2))
print(my_t.index(3))

Output:
(1,2,3,4)
2
2

DICTIONARY:

 Dictionaries are used to store data values in key:value pairs.


 A dictionary is a collection which is ordered*, changeable and do not allow
duplicates.
 Dictionaries are written with curly brackets, and have keys and values.

Creating a Dictionary:
 Dictionary normally creating using object. Also, it will create using curly
braces {}.
 All the elements of the dictionary are key and value pairs. Like list
dictionary also creates as empty.
Example:
dict1={}
print(dict1)
dict2={'a':"apple", 'b':"banana"}
print(dict2)
Output:
{}
{'a':" apple", 'b':" banana"}

METHODS IN DICT:
 Adding an item to the dictionary is done by using a new index key and
assigning a value to it:
 Delete the elements in the collection also uses the following methods.
 pop() - It will remove the element in the specified key value.
 popitem() - It will remove the first set of key-value pairs in the dictionary.
It also returns the elements in a tuple.
 clear() - it clears the all key-value pair of elements.
 Del()- it will delete the specified key-value pairs.

EXAMPLE:
d={1:"apple",2:"ball"}
print(d)
d[3]="python"
print(d)
d[1]="ruby"
print(d)
del d[1]
print(d)
d1={4:"reya",5:"siri",7:"alex"}
print(d1.popitem())
d1.clear()
print(d1)

OUTPUT:
{1: 'apple', 2: 'ball'} # printing dict

{1: 'apple', 2: 'ball', 3: 'python'} # adding 3:keyvalue

{1: 'ruby', 2: 'ball', 3: 'python'} #changing the value in 1 st key


{2: 'ball', 3: 'python'} # deleting the 1 keyvalue pair
(7, 'alex') # after popitem last pair will be in tuple
{} #after clear

Accessing the Elements:


Access to the elements of the dictionary will be done using keys like an index. And
also we use the method called get(). Just pass the key as a parameter to receive
the element value.
Example:
m_dict = {'a":"ant", 'b' : "bee", 'c': "cat"}
print(m_dict)
print(m_dict['b'])
print(m_dict.get('c'))
Output:
{'b': 'bee', 'c': 'cat', 'a': 'ant'}
bee
cat

Other Functions:
Apart from the above manipulation methods, we have some different methods in
the dictionary as follows.
 Items() - It will return all the key-value pairs of elements in a list of tuples.
 keys() - It will return only the keys as a list.
 values() It will return the values as a list.
Example:
dict = {1:"ant", 2 : "bee", 3: "cat"}
print(dict)
print(dict.items())
print(dict.keys())
print(dict.values())
Output:
{1: 'ant', 2: 'bee', 3: 'cat'}
dict_items([(1, 'ant'), (2, 'bee'), (3, 'cat')])
dict_keys([1, 2, 3])
dict_values(['ant', 'bee', 'cat'])

SETS:
 Sets in Python are a mutable collection of elements unordered.
 Also sets not allows duplicates.i.e if you insert the same element in more
than one time even it will take only once.
 This set operations are moreequal to the arithmetic sets in mathematics.
Creating a Sets
Sets are created using curly or flower braces in python. It doesn't allow
duplicates. i.e values are unique.
Example:
my_s={1,2,3,4,3,5,2}
print(my_s)
Output:
{1,2,3,4,5}

Adding Elements
 Add the elements in the sets using add() method.
Example:
my_s={1,2,4,2}
my_s.add(3)
print(my_s)
Output:
{1,2,3,4}

Other Operations in Sets :


The different operation methods of the sets in follows.
→ union() - It used to do the unit operations of the set. Just concatenate the two
sets without duplicates.
 intersection() – It returns the common values of both sets.
→ difference() - It returns the difference values in the passed set. And also deletes
the data represent in both sets.
→ symmetric_difference() - It returns same process of difference() method. But it
will return the which data remaining in both sets.

Example:
my_s1={1,2,3,4}
my_s2={3,4,5,6}
print(my_s1.union(my_s2))
print(my_s1.intersection(my_s2))
print(my_s1.difference(my_s2))
print(my_s1.symmetric_difference(my_s2))

Output:
{1,2,3,4,5,6}
{3,4}
{1,2}
{1,2,5,6}

You might also like