UNIT-4 (1)
UNIT-4 (1)
COMPOUND DATA
LISTS,TUPLES,DICTIONARIES
LIST DEFINITION
• A list is an ordered set of values, where each
value is identified by an index.
• The values that make up a list are called its
elements.
• Lists and strings and other things that behave
like ordered sets are called sequences.
• Updating in the existing list is possible.
EXAMPLES
• LIST1=[“maths”,”physics”,”chemistry”]
• LIST2=[1,2,3,4,5]
• WORDS=[“ameliorate”, “castigate”]
• numbers=[17, 123]
ACCESSING ELEMENTS IN LIST
>>>LIST2=[1,2,3,4,5]
>>> print(LIST2[0])
1
>>>print(LIST2[-1])
5
>>>WORDS=[“hello”, “hai”]
>>>print (WORDS[1])
hai
UPDATING IN A LIST
• In list you can add the element using the
method append()
• list.append(obj)
Example
>>> list2=[1,2,3,4,5]
>>>list2.append(9)
>>>print (list2[])
[1,2,3,4,5,9]
Delete list elements
• To remove a element in a list, you can use either del statement
if you exactly know which elements you are deleting or
remove() method if you don’t know.
• Example
>>>numbers=[17, 123,54,78,99,45,67]
>>>del list[2]
>>>print(numbers[])
[17, 123,78,99,45,67]
>>>numbers.remove(123)
>>>print(numbers[])
[17,78,99,45,67]
List operations
• Lists respond to the + and * operators much
like strings; they mean concatenation and
repetition here too, except that the result is a
new list, not a string.
BASIC LIST OPERATIONS
PYTHON EXXPRESSION RESULTS DESCRIPTION
Len([1,2,3]) 3 length
[1,2,3]+[4,5] [1,2,3,4,5] concatenation
[‘hi’]*2 [‘hi’ ‘hi’] repetition
2 in [1,2,3], True
5 not in [1,2,4] True Membership
• Tour=[‘chennai’,’mumbai’,’banglore’,’australia’]
for t in tour
print(t)
output:
chennai
mumbai
banglore
australia
Lists are mutable
• lists are mutable, which means we can change
their elements.
Example
List Deletion
ALIASING
• if we assign one variable to another, both
variables refer to the same object.
>>>a=[13,14,6,8,9]
>>>b=a
>>>a is b
true
CLONING LISTS
• If we want to modify a list and also keep a
copy of the original, we need to be able to
make a copy of the list itself, not just the
reference.
• This process is sometimes called cloning, to
avoid the ambiguity of the word copy.
List parameters
• Passing a list as an argument actually passes a
reference to the list, not a copy of the list.
Example: def double (a_list):
Tuples
• A tuple is a sequence of immutable Python objects.
• Tuples are sequences, just like lists.
• Examples
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = ("a", "b", "c", "d“)
Accessing Values in Tuples
• To access values in tuple, use the square brackets for slicing
along with the index or indices to obtain value available at that
index.
• Example
>>>tup1 = ('physics', 'chemistry', 1997, 2000)
>>> tup2 = (1, 2, 3, 4, 5, 6, 7 )
>>>print ("tup1[0]: ", tup1[0] ) #for positive indexing
>>>print ("tup2[1:5]: ", tup2[1:5]) # for slicing
Output:
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples
my_tuple = ('a','p','p','l','e',)
# In operation
# Output: True
print('a' in my_tuple)
# Output: False
print('b' in my_tuple)
# Not in operation
# Output: True
print('g' not in my_tuple)
Comparing tuples
• The comparison operators work with tuples
and other sequences.
• Python starts by comparing the first element
from each sequence.
Decorate
• A sequence by building a list of tuples with
one or more sort keys preceding the elements
from the sequence
Undecorate
• By extracting the sorted elements of the
sequence.
TUPLES AS RETURN VALUES
• a function can only return one value, but
if the value is a tuple, the effect is the same as
returning multiple values.
Example
DICTIONARIES
Definition for dictionaries
• Python dictionary is an unordered collection
of items.
• While other compound data types have only
value as an element, a dictionary has a key:
value pair.
• Dictionaries are enclosed by curly braces ({ })
and values can be assigned and accessed using
square braces ([]).
# empty dictionary
my_dict = {}
# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'}
# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])
Access elements from a dictionary
del dict['Name'];
# remove entry with key 'Name'
dict.clear();
# remove all entries in dict
del dict ;
# delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
Methods in dictionaries
Advanced List Processing-list Comprehension
Illustrative programs
Revised with lab programs
Merge sort
Insertion sort
Selection sort
Quick sort
THANK YOU