0% found this document useful (0 votes)
3 views

Unit 4 Basic Data Structures

IT fundamentals of programming

Uploaded by

kayelpat2708
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 4 Basic Data Structures

IT fundamentals of programming

Uploaded by

kayelpat2708
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Basic Data Structures

CC S 1 4 0 0
Unit 4
❑ Data Structures
− way of organizing data so that it can be accessed more
efficiently
− “containers” that organize and group data according to type

2
Basic Python Data Structures
❑ List
− a collection which is ordered, changeable, and allows
duplicate members
❑ Tuple
− a collection which is ordered, unchangeable, and allows
duplicate members
❑ Set
− a collection which is unordered, unchangeable, unindexed
and does not allow duplicate members
• items are unchangeable, but items can be added or removed
❑ Dictionary
− a collection which is ordered, changeable, and does not allow
duplicate members
• as of Python version 3.7, dictionaries are ordered

3
List
− just like the arrays declared in other languages, which is an
ordered collection of data
− very flexible because it can contain different data types

4
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified
value
extend() Add the elements of a list (or any iterable), to the end
of the current list
index() Returns the index of the first element with the specified
value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
5
Tuple
− a collection of Python
objects like a list but
are immutable in
nature

6
7
Method Description
count() Returns the number of times a specified value
occurs in a tuple
index() Searches the tuple for a specified value and returns
the position of where it was found

8
Set
− an unordered
collection of data that
is immutable and does
not allow any duplicate
element
− used to include
membership testing
and eliminating
duplicate entries
− can contain different
data types

9
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference
between two or more sets
difference_update() Removes the items in this set that are also
included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two
other sets
intersection_update() Removes the items in this set that are not
present in other, specified set(s)

10
Method Description
isdisjoint() Returns whether two sets have a
intersection or not
issubset() Returns whether another set contains
this set or not
issuperset() Returns whether this set contains
another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric
differences of two sets
symmetric_difference_update() inserts the symmetric differences from
this set and another
union() Return a set containing the union of
sets
update() Update the set with the union of this set
and others
11
Dictionary
− an ordered collection of data values
− holds the key:value pair
• key - an object which can never change like strings, numbers,
tuples, etc.
− cannot have two items with the same key

12
13
14
Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does
not exist: insert the key, with the specified value
update() Updates the dictionary with the specified key-value
pairs
values() Returns a list of all the values in the dictionary

15

You might also like