04 Python Data Structures
04 Python Data Structures
Tushar B. Kute,
http://tusharkute.com
Data Structures
Data Structures Separators
List [ ]
Tuple ( )
Set { }
Dictionary { : }
List
>>> days
['Mon', 'Tue', 'Wed', 4, 5, 6]
>>> print(lang)
['Mar', 'Urd', 'San', 'Guj', 'Hin']
• Indexing Starts from 0 to n-1
>>> print(lang[2])
San
>>> lang[1] = 'Kan'
>>> print(lang)
['Mar', 'Kan', 'San', 'Guj', 'Hin']
Negative Indexing
7 5 3
List comprehension
• len( )
– It calculates the length of the list.
• max( )
– It returns the item from the list with the highest
value.
• min( )
– It returns the item from the Python list with the
lowest value.
• sum( )
– It returns the sum of all the elements in the list.
Example:
• sorted( )
– It returns a sorted version of the list, but does not
change the original one.
• list( )
– It converts a different data type into a list.
• any( )
– It returns True if even one item in the Python list has a
True value.
• all( )
– It returns True if all items in the list have a True value.
Sort
>>> list('xyz')
['x', 'y', 'z']
>>> data = [0,0,0,0,False]
>>> any(data)
False
>>> data = [0,0,3,0,False]
>>> any(data)
True
all( )
arr = []
n = int(input('How many elements? '))
for x in range(n):
num = int(input('Enter number:'))
arr.append(num)
• len( )
– It calculates the length of the tuple.
• max( )
– It returns the item from the tuple with the highest
value.
• min( )
– It returns the item from the tuple with the lowest
value.
• sum( )
– It returns the sum of all the elements in the tuple.
Example:
• sorted( )
– It returns a sorted version of the tuple, but does not
change the original one.
• tuple( )
– It converts a different data type into a tuple.
• any( )
– It returns True if even one item in the tuple has a True
value.
• all( )
– It returns True if all items in the tuple have a True value.
Sort
>>> tuple('xyz')
('x', 'y', 'z')
>>> data = (0,0,0,0,False)
>>> any(data)
False
>>> data = (0,0,3,0,False)
>>> any(data)
True
all( )
• index()
– This method takes one argument and returns the
index of the first appearance of an item in a
tuple.
>>> per.index(63)
• count()
– This method takes one argument and returns the
number of times an item appears in the tuple.
>>> per.count(85)
More operations
• Membership :
>>> 56 in per
False
>>> 81 not in per
True
• Concatenation:
>>> per + (18,43)
(67, 85, 69, 72, 63, 18, 43)
>>> new = per + (18,43)
>>> new
(67, 85, 69, 72, 63, 18, 43)
More operations
• Iteration:
>>> per
(67, 85, 69, 72, 63)
>>> for n in per:
print(n, sep=' ')
• Nesting:
>>> num = (23,(2,7),11,36)
>>> num[1]
(2, 7)
>>> num[1][0]
2
Sample list of tuple
Example:
>>> d = {'a':'apple','b':'ball'}
>>> num = {1:25, 3:10, 5:11}
>>> type(d)
<class 'dict'>
>>> type(num)
<class 'dict'>
>>> num = {1:25, 3:10, 5:11, 'x':'data'}
>>> type(num)
<class 'dict'>
Accessing and adding elements
>>> d['a']
'apple'
>>> num[3]
10
>>> num[2] = 20
>>> num
{1: 25, 2: 20, 3: 10, 5: 11, 'x':
'data'}
Using dict( ) function
>>> animals={}
>>> type(animals)
<class 'dict'>
>>> animals[1]='dog'
>>> animals[2]='cat'
>>> animals[3]='ferret'
>>> animals
{1: 'dog', 2: 'cat', 3: 'ferret'}
Dictionary methods
• keys( )
– The keys() method returns a list of keys in a
Python dictionary.
• values( )
– Likewise, the values() method returns a list of
values in the dictionary.
• items( )
– This method returns a list of key-value pairs.
Dictionary methods
• get()
– It takes one to two arguments. While the first
is the key to search for, the second is the
value to return if the key isn’t found. The
default value for this second argument is
None.
• clear( )
– The clear function’s purpose is obvious. It
empties the Python dictionary.
Methods
>>> data
{9: 20, 2: 45, 5: 38, 6: 11, 7: 105}
>>> data.get(2,1)
45
>>> data.get(4,5)
5
>>> data.get(7,110)
105
Methods
• copy()
– The copy() method creates a shallow copy of
the Python dictionary.
• pop()
– This method is used to remove and display an
item from the dictionary. It takes one to two
arguments. The first is the key to be deleted,
while the second is the value that’s returned
if the key isn’t found.
Thank you
This presentation is created using LibreOffice Impress 5.1.6.2, can be used freely as per GNU General Public License
Web Resources
http://mitu.co.in
http://tusharkute.com
contact@mitu.co.in
tushar@tusharkute.com