Python Lists Tuple and Dictionary
Python Lists Tuple and Dictionary
DICTIONARY
LISTS
What is a List?
A list is a collection which is ordered and changeable. In Python lists are written with
square brackets.
Example
To Create a List:
# list of integers
my_list = [1, 2, 3]
Example
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
# Nested List
n_list = ["Happy", [2,0,1,5]]
# Nested indexing
# Output: a
print(n_list[0][1])
# Output: 5
print(n_list[1][3])
Negative indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2
to the second last item and so on.
my_list = ['p','r','o','b','e']
# Output: e
print(my_list[-1])
# Output: p
print(my_list[-5])
We can access a range of items in a list by using the slicing operator (colon).
my_list = ['p','r','o','g','r','a','m','i','z']
# elements 3rd to 5th
print(my_list[2:5])
You can loop through the list items by using a for loop:
Example
To add an item to the end of the list, use the append() method:
Example
Example
Example
The pop() method removes the specified index, (or the last item if index is not
specified):
Copying Lists
a=[10,20,30,40,50]
b=a
print a
print b
a[3]=45
print a
print b
# the above method shows that a is copied to b and if something modified in
# a will get refleted in b
# when both lists to be treated separately then the following method will be used
b=list(a)
a[0]=12
print a
print b
List Functions
a=[10,20,30,40]
del a[1]
print a
a.pop(0) # pop and del will do the same.in del method index is mandatory
print a # in pop index is optinal if skipped then the last value will be deleted
k=a.pop()
print a
print k
# In case if we dont know the index position then the element will be removed with
the help of element (value) directly
a1=[100,200,300,400]
a1.remove(200)
print a1
a1.reverse()
print a1
z=['h','n','g','f','y']
z.sort()
print z
z.reverse()
print z
Program to eliminate duplicate values
d=[12,4,5,12,6,4,7,5]
list1 = []
for num in d:
if num not in list1:
list1.append(num)
print(list1)
Palindrome
a=['racecar','amma','sam','abba','ram']
for i in a:
if i[0]==i[-1]:
print(i)
Slicing: l=[12,3,4,5,6,7,8,9]
print(l[::]) [12, 3, 4, 5, 6, 7, 8, 9]
print(l[:]) [12, 3, 4, 5, 6, 7, 8, 9]
print(l[:3]) [12, 3, 4]
print(l[0:3]) [12, 3, 4]
print(l[1:5]) [3, 4, 5, 6]
print(l[::2]) [12, 4, 6, 8]
print(l[::-1]) [9, 8, 7, 6, 5, 4, 3, 12]
print(l[::-2]) [9, 7, 5, 3]
print(l[-5:]) [5, 6, 7, 8, 9]
print(l[-6:-2]) [4, 5, 6, 7]
print(l[6:1:-1]) [8, 7, 6, 5, 4]
Tuples
Tuples are immutable
#create a tuple
tuplex = "w", 3, "r", "s", "o", "u", "r", "c", "e"
print(tuplex)
t1=(12,3,5,6,66.7)
print (t1)
print len(t1)
print max(t1)
print (min(t1))
my_tuple = ('a','p','p','l','e',)
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
Dictionary
Unordered collection of data . Used to store data like a map
# You may also use different types other than strings for key/value pairs.
# Assigns an integer value to the 'key2' key.
d['key2'] = 1234567890
sp = {}
sp["one"] = "uno"
sp["two"] = "dos"
for k in sp.keys():
other functions
d1={'name': 'Ram', 'roll': 101, 'mark': 80}
print len(d1)
#del will delete the particular item . And the argument will be the key
del d["name"]
#del will delete the particular item . And the argument will be the key
d1.pop('roll')
print (dict1)
pop() can remove any item from a dictionary as long as you specify the key.
emp3={'Rama':'TT','Rini':'Basketball','Raghu':'Athletis'}
emp2.update(emp3)
print (emp2)