datastructuresinpython-3-4
datastructuresinpython-3-4
Python
By – Tanmay Jain
Types of data structures
• List notation
• A = [1,”This is a list”, ‘c’,2.5]
• B = []
• C = list()
Create a List
• Make a list using string
• Read one :
>> names[0]
>> Rahul
>> names[-1]
>> Aishwarya
• List.append(x)
– Adds an item to the end of the list
Eg:
>> list_items = [1, 2, 3, 4, 5]
>> list_items.append(6)
>> list_items
>> [ 1, 2, 3, 4, 5, 6 ]
• List.extend(L)
- Extends the list by appending all the items in the given list
‘L’ to the existing list
Eg:
>> list_items = [1, 2, 3, 4, 5]
>> list_items.extend([6, 7, 8, 9])
>> list_items
>> [ 1, 2, 3, 4, 5, 6, 7, 8 , 9 ]
• List.insert(i,x)
- Inserts an item x at
index i
Eg:
>> list_items = [1, 2, 3, 4, 5]
>> list_items.insert(3, 10)
>> list_items
>> [ 1, 2, 3, 10, 4, 5]
• List.remove(x)
- Removes the first occurrence of the item from the list
whose value is x
Eg:
>> list_items = [1, 5, 3, 4, 5, 5]
>> list_items.remove(5)
>> list_items
>> [ 1, 3, 4, 5, 5]
• List.pop(i)
- Remove and returns item at index i,default value of i is last index of the
list Eg:
>> list_items = [1, 5, 3, 4, 5, 8]
>> list_items.pop()
>> 8
>> lst
>> [1, 5, 3, 4, 5]
>> list_items.pop(2)
>> 3
>> lst
[1, 5, 4, 5]
Some other methods of
Lists
>> a = [1, 2, 3, 4, 5, 6, 7, 6]
• a.count(x) # Returns occurrence of specified x
>> a.count(6)
>> 2
• a.index(x) # Returns the first index where the given value appears
>> a.index(6)
>> 5
• a.reverse() # Reverses order of list
>> a.reverse()
>> [6, 7, 6, 5, 4, 3, 2, 1]
• a.sort()
>> a
>> [1, 2, 3, 4, 5, 6, 6, 7]
Slicing a List
• List[ start, stop]
>> lst[6:10]
>> ['P', 'y', 't', 'h']
>> lst[0 : 5]
>> ['M', 'o', 'n', 't', 'y']
>> lst[6: 10] >> lst[:5]
>> [''P', 'y', 't', 'h', 'o'] >> ['M', 'o', 'n', 't', 'y']
Input will be items separated by space. Perform the following actions. a).
Make a list of the input provided and print it
b) Count the no of items in the list and print it
c)Ask the user to provide a item as input and find the index of the item , if the item is not
present print ‘Item not found’ else print the index.
Find the Occurrence of the item in the list d).
Reverse the list and print it
e). Sort the list in descending order and print
the sorted list
Input :
Enter the numbers :a c d e z k m o
Practice
Diction
• Consists of Key– Value pair ary
• Keys needs to unique
• Items of the dictionary are not ordered
Eg:
>> empty_dict = dict()
>> empty_dict
>> {}
>> Phonebook['Rock‘]
>> 999999999
Modifying a Dictionary
>> phonebook = {'Rock': 999999999, 'Rashmi': 888888888, 'Mohan': 777777777}
>> phonebook['Rock‘] = 666666666
>> phonebook
>> {'Rock': 666666666, 'Rashmi': 888888888, 'Mohan': 777777777}
dict.values()
>> phonebook = {'Rock': 999999999, 'Rashmi': 888888888, 'Mohan': 777777777}
>> phonebook.values()
>> [777777777, 888888888, 999999999]
• dict.items()
Output:
{‘Mahesh’ : 20 }
Practice
2. Write a program to read the names from the user and make a list of
names , then loop through the list of name and ask for email.
Make a dictionary of name,email
Sample Input :
Output:
Tuples
>> a
=1
>> b
=2
>>
a, b
= b,
a
Set
• Sets are unordered collections of simple objects
• Unique collections of immutable objects
• Define a Set:
>> x = {"a","b","c","d","e"}
>> y = {"b","c"}
>> x.difference(y)
>> set(['a', 'e', 'd'])
• discard(ele)
>> x = {"a","b","c","d","e"}
>> x.discard("b")
>> x
>> set(['a', ‘c', 'e', 'd'])
• remove(ele)
>> x = {"a","b","c","d","e"}
>> x.remove("b")
>> x
>> set(['a', ‘c', 'e', 'd'])
• intersection
>> x = {"a","b","c"}
>> y = {"d","e"}
>> x.intersection(y)
>> set([])
>> y.add("b")
>> x.intersection(y)
>> set(["b"])
• union
>> x = {"a","b","c"}
>> y = {"d","e"}
>> x.union(y)
• issubset
>>> x = {"a","b","c"}
>>> y = {"d","e"}
>>> z = {"b","c"}
>>> y.issubset(x)
False
>>> z.issubset(x)
True
• issuperset
>>> x = {"a","b","c","d"}
>>> y = {"c", "d"}
>>> x.issuperset(y)
True
>>> y.issuperset(x)
False
• pop()
>>> x = {"a","b","c","d"}
>>> x.pop()
>>> ‘a’
Questions ?