[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

List and Set Methods

The document provides a comprehensive list of methods for manipulating lists and sets in Python, including their descriptions, syntax, and examples. For lists, methods such as append(), clear(), and sort() are detailed, while for sets, methods like add(), difference(), and union() are explained. Each method is accompanied by its syntax and an illustrative example for clarity.

Uploaded by

panupama1256
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)
4 views2 pages

List and Set Methods

The document provides a comprehensive list of methods for manipulating lists and sets in Python, including their descriptions, syntax, and examples. For lists, methods such as append(), clear(), and sort() are detailed, while for sets, methods like add(), difference(), and union() are explained. Each method is accompanied by its syntax and an illustrative example for clarity.

Uploaded by

panupama1256
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/ 2

List Methods

Method Description Syntax Example

L1 = [1,2.5,’abcd’,1,2,3]
L2=[4,5,6,’xyz’]
append() Adds an element at the end of the list list.append(elmnt) L1.append(True)
clear() Removes all the elements from the list.clear() L1.clear()
list
copy() Returns a copy of the list list.copy() L3=L1.copy()
count() Returns the number of elements with list.count(value) L1.count(1)
the specified value
extend() Add the elements of a list (or any list.extend(iterable) L1.extend(L2)
iterable), to the end of the current list any iterable (list, set,
tuple, etc.)
index() Returns the index of the first element list.index(elmnt) print(L1.index(‘abcd’))
with the specified value
insert() Adds an element at the specified list.insert(pos, elmnt) L1.insert(2,True)
position
pop() Removes the element at the specified list.pop(pos) L1.pop(4)
position
remove() Removes the first item with the list.remove(elmnt) L1.remove(‘abcd’)
specified value
reverse() Reverses the order of the list list.reverse() L1.reverse()
sort() Sorts the list list.sort(reverse=True|Fa L1.sort()
lse, key=myFunc)

Set Methods

Method Description Syntax Example


S1 = {1,2,3,4,5}
S2={4,5,6}
add() Adds an element to the set set.add(elmnt) S1.add(7)
clear() Removes all the elements from set set.clear() S1.clear()
copy() Returns a copy of the set set.copy() S3=S1.copy()
difference() Returns a set containing the set1.difference(set2,set3 S3=S1.difference(S2)
difference between two or more ,…)
sets
difference_up Removes the items in this set that set1.difference_update(s S1.difference_update(S2)
date() are also included in another, et2,set3,…)
specified set
discard() Remove the specified item set.discard(elmnt) S1.discard(4)
pop() set.pop() S1.pop()
Removes random item from the set

remove() set.remove() S1.remove(3)


Removes the specified element.
This method will raise an error if
the specified item does not exist

intersection() Returns a set, that is the set1.intesection(set2,set S3=S1.intersection(S2)


intersection of two or more sets 3,…)
Intersection_ Removes the items in this set that set1.intersection_update S1.intersection_update(S2
update() are not present in other, specified ( )
set(s) set2,set3,…)
symmetric_di Returns a set that contains all items set1.symmetric_differen S3=S1.symmetric_differe
fference() from both sets, except items that ce(set2,set3,….) nce(S2)
are present in both sets
symmetric_di Remove the items that are present set1.symmetric_differen S1.symmetric_difference_
fference_upd in both sets, AND insert the items ce_update(set2,set3,….) update(S2)
ate() that is not present in both sets
union() Return a set containing the union of set1.union(set2,set3,..) S3=S1.union(S2)
sets
update() Update the set with another set, or set1.update(set2) S1.update(S2)
any other iterable
isdisjoint() Returns whether two sets have a set1.isdijoint(set2) print(S1.isdisjoint(S2))
intersection or not
issubset() Returns whether another set set1.issubset(set2) print(S1.issubset(S2))
contains this set or not
issuperset() Sorts the list set1.issuperset(set2) print(S1.issuperset(S2))

You might also like