0% found this document useful (0 votes)
6 views

List and Dictionary Functions

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

List and Dictionary Functions

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

List Operators

Python Results Description


Expression
len([1, 2,3]) 3 Length
[1, 2, 3] + [4, 5,6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!','Hi!', Repetition
'Hi!']
3 in [1, 2,3] True Membership
for x in [1, 2,3]: 1 23 Iteration
printx,

L=[24,35,46,78]
Python Results Description
Expression
L[2] [46] Offsets start at
zero

L[-3] [35] Negative: count


from the right

L[1:] [35,46,78] Slicing fetches


sections

List General Functions


Sr.No. Function with Description

1 len(list)Gives the total length of the list.

2 max(list)Returns item from the list with max value.


3 min(list)Returns item from the list with min value.

4 list(seq) Converts a tuple or a string in to list.

List specific functions


Sr.No. Methods withDescription

1 list.append(obj)Appends object obj to list


2 list.count(obj)Returns count of how many times obj occurs in
list
3 list.extend(seq)Appends the contents of seq to list
4 list.index(obj) Returns the lowest index in list that obj appears

5 list.insert(index,obj)Inserts object obj into list at offset index

6 list.pop(obj=list[-1])Removes and returns last object or object


at obj from list
7 list.remove(obj)Removes object obj from list
8 list.reverse()Reverses objects of list in place
9 list.sort([func]) Sorts objects of list

Python Dictionary Methods


Method Description
clear() Removes all items from the dictionary.
copy() Returns a shallow copy of the dictionary.
fromkeys(seq[, v]) Returns a new dictionary with keys from seq and value equal
to v (defaults to None).
get(key[,d]) Returns the value of the key. If the key does not exist, returns
d (defaults to None).
items() Return a new object of the dictionary's items in (key, value)
format.
keys() Returns a new object of the dictionary's keys.
pop(key[,d]) Removes the item with the key and returns its value or d if
key is not found. If d is not provided and the key is not found,
it raises KeyError.
popitem() Removes and returns an arbitrary item (key, value). Raises
KeyError if the dictionary is empty.
setdefault(key[,d]) Returns the corresponding value if the key is in the dictionary.
If not, inserts the key with a value of d and returns d (defaults
to None).
update([other]) Updates the dictionary with the key/value pairs from other,
overwriting existing keys.
values() Returns a new object of the dictionary's values

You might also like