List, Tuple, String, Dictionary Functions
List, Tuple, String, Dictionary Functions
1. len() Returns the length of its argument list. It’s a python standard
library function.
l=[1,2,3]
len(l)🡪will return 3
a=list(“hello”)
print(a)
[‘h’,’e’,’l’,’l’,’o’]
3. index() It returns the index of the first matched item from the list.
Syntax:
list.index(item)
eg:
L=[13,18,11,16,18,14]
L.index(18)
output will be 1
Syntax:
list.append(item)
Eg:
colours=['red','green','yellow']
colours.append('blue')
print(colours)
Syntax:
list.extend(list)
eg:
t1=['a','b','c']
t2=['d','e']
t1.extend(t2)
print(t1)
6. insert()
● It is also an insertion method for list
● Both append and extend inserts elements at the end.
t=['a','e','u']
t.insert(2,'i')
print(t)
7. pop() It is used to remove the item from the list. Takes one optional
argument and returns a value(the item being deleted)
Syntax:
list.pop(index)
L=[1,2,3,4,5]
val=L.pop(0)
print(val)
print(L)
[2, 3, 4, 5]
L=[1,2,3,4,5]
val=L.pop()
print(val)
print(L)
[1, 2, 3, 4]
8. remove() It removes the first occurrence of given item from the list. It
doesn’t return anything and takes one essential argument.
Syntax:
List.remove(value)
L=['a','e','i','o','u']
L.remove('a')
print(L)
OUTPUT will be
['e', 'i', 'o', 'u']
l=['a','p','b','c','d','p']
l.remove('p')
print(l)
OUTPUT will be
['a', 'b', 'c', 'd', 'p']
9. clear() It removes all the items from the list and the list becomes
empty list after this function. It doesn’t return anything.
Syntax:
list.clear()
l=[1,2,3,4]
l.clear()
print(l)
10. count() It returns the count of the item that you passed as argument.
If the given item is not in the list it returns zero.
Syntax:
list.count(item)
Eg:
L=[13,18,20,10,18,23]
L.count(18)
OUTPUT will be 2
L.count(28)
OUTPUT will be 0.
Syntax:
List.reverse()
Eg:
l=[1,2,3,4]
l.reverse()
print(l)
12. sort() It sorts the items of the list by default in increasing order.
Syntax:
list.sort([reverse=False/True])
L=[20,1,16,8,25]
L.sort()
print(L)
L=[20,1,16,8,25]
L.sort(reverse=True)
print(L)
13. min() The min(),max() and sum() functions take the list sequence
and return the minimum element, maximum element and
max()
the sum of elements of the list respectively.
sum()
Syntax:
min(list)
max(list)
sum(list)
Eg:
L=[20,1,16,8,25]
print(min(L))
print(max(L))
print(sum(L))
25
70
Tuple Functions
1. len() Eg: t=(1,2,3,4)
It returns the length of the tuple. ie counts print(len(t))
the no:of elements in the tuple.
Output will be:
syntax:
4
len(tuple)
2. max() Eg:
It returns the element from tuple with t=(2,90,45,8)
maximum value.
print(max(t))
Values in the tuple must be of same type.
Output will be:
Syntax: 90
max(tuple)
3. min() Eg:
It returns the element from tuple with t=(2,90,45,8)
minimum value.
print(min(t))
Values in the tuple must be of same type. Output will be:
Syntax: 2
min(tuple)
4. sum() Eg:
It takes tuple as parameter and returns the t=(51,2,91,6)
sum of elements of the tuple.
sum(t)
Output is
150
5. index() Eg:
It returns the index of an existing element of t=(1,2,3,4)
a tuple.
t.index(3)
Syntax:
Output will be:
tuple.index(item)
2
6. count() Eg:
It returns the count of a element in a given t=(1,2,3,1,2,4,1,2,3)
sequence.
t.count(2)
Syntax:
Output will be:
tuple.count(element)
3
7. sorted() Eg:
It takes name of the argument and returns a t=(51,2,91,6)
new sorted list with sorted elements in it.
t1=sorted(t)
Syntax:
print(t1)
sorted(tuple,[reverse=False])
Output will be:
This sorts is ascending order. [2, 6, 51, 91]
8. tuple() Eg:
It is used to create tuples from different type Empty tuple
of values.
t=()
Syntax:
print(t)
tuple(Sequence)
Output will be:
()
t=tuple("abc")
print(t)
Output will be:
('a', 'b', 'c')
4. keys() Eg:
● It returns all the keys in d={1:10,2:20,3:30,4:40}
the dictionary as a d.keys()
sequence of keys. Output will be:
dict_keys([1, 2, 3, 4])
syntax:
dictionary.keys()
5. values() Eg:
● It returns all the values in d={1:10,2:20,3:30,4:40}
the dictionary as a d.values()
sequence of keys. Output will be:
syntax: dict_values([10, 20, 30, 40])
dictionary.values()
6. update() Eg:
● This merges key:value d={1:10,2:20,3:30}
pairs from the new d1={4:40,5:50}
dictionary in to the d.update(d1)
original dictionary adding print(d)
or replacing as needed. print(d1)
● The items in the new Output will be:
{1: 10, 2: 20, 3: 30, 4: 40, 5:
dictionary are added to 50}
the old one and override {4: 40, 5: 50}
13 sorted() Eg:
. ● It considers only the keys d={90:"abc",45:"def",2:"pqr",92:"hij"
of the dictionary for }
sorting and returns a d1=sorted(d)
sorted list of the print(d1)
dictionary keys output:
[2, 45, 90, 92]
Syntax: d={90:"abc",45:"def",2:"pqr",92:"hij"
sorted(dictionary,reverse=Fal }
se) d1=sorted(d,reverse=True)
if reverse argument is true, print(d1)
then it returns tge keys of output will be:
the dictionary sorted in [92, 90, 45, 2]
descending order.
● it returns the result always
in a list.
● all keys must be of same
type for this function to
work.
14 max(),min() and sum() eg:
. ● dictionary key must be of d={1:10,2:90,80:50,75:9}
homogenous type. print(min(d))
syntax: print(max(d))
max(dictionary) print(sum(d))
min(dictionary) Output will be:
1
sum(dictionary) 80
158
● sum() can only work with
dictionaries having keys
which are addition
compatable.
1. len() eg:
Returns the length of its argument string len(“hello”)
Syntax: will return 5
len(string) (Total no:of characters in the
string)
2. capitalize() Eg:
It returns a copy of the string with its first ‘i love my India’.capitalize()
character capitalized
will return
Syntax:
I love my India
string.capitalize()
3. lower() Eg:
It returns a copy of the string converted to “HELLO”.lower()
lowercase
‘hello’
Syntax:
“hi”.lower()
string.lower()
‘hi’
4. upper() eg:
It returns a copy of the string converted to ‘hello’.upper()
uppercase
‘HELLO’
Syntax:
“HI”.upper()
string.upper() “HI”
5. count() Eg:
It returns the number of occurrences of the ‘abracadabra’.count(‘ab’)
substring sub in string[or string[start: end] if these
It counts the no: of
arguments are given.
occurrences of ‘ab’ in the
Syntax: whole string. So it returns 2.
string.count(sub[,start[,end]])
7. index() Eg:
8. isalnum() Eg:
It returns True if characters in the string are s=’abc123’
alphanumeric (alphabets or numbers) and there is s.isalnum() returns True
at least one character, False otherwise.
s=’123’
Syntax:
s.isalnum() returns True
string.isalnum()
s=’ ‘
s.isalnum() returns False
9. isalpha() Eg:
It returns True if all characters in the string are s=’abc123’
alphabetic and there is at least one character,
s.isalpha() returns False
False otherwise.
s=’hello’
Syntax:
s.isalpha() returns True
string.isalpha()
Syntax: “H**e**l**l**o”
string.join(String iterable)
Syntax:
string.partition(separator/string)