0% found this document useful (0 votes)
1 views1 page

Python Mutable vs Immutable Objects 1675358358

The document outlines various operations that can be performed on mutable objects in Python, such as lists, dictionaries, and sets. It details how these operations affect the objects, including item replacement, deletion, appending, and copying. The document emphasizes the mutability of these objects through specific methods and their behaviors.

Uploaded by

suno
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)
1 views1 page

Python Mutable vs Immutable Objects 1675358358

The document outlines various operations that can be performed on mutable objects in Python, such as lists, dictionaries, and sets. It details how these operations affect the objects, including item replacement, deletion, appending, and copying. The document emphasizes the mutability of these objects through specific methods and their behaviors.

Uploaded by

suno
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/ 1

How to identify whether an object (list, dictionary, tuple, set,

string, etc.) is mutable (instead of immutable) in Python


Operation Results Notes
s[i] = x item i of s is replaced by x

slice of s from i to j is replaced by


s[i:j] = t
the contents of the iterable t

del s[i:j] same as s[i:j] = []

the elements of s[i:j:k] are t must have the same length as the slice it is
s[i:j:k] = t
replaced by those of t replacing.

removes the elements of


del s[i:j:k]
s[i:j:k] from the list

appends x to the end of


s.append(x) the sequence (same as
s[len(s):len(s)] = [x])
Included for consistency with the interfaces of
removes all items from s (same
s.clear() mutable containers that don’t support slicing
as del s[:]) operations (such as dict and set).

Included for consistency with the interfaces of


mutable containers that don’t support slicing
creates a shallow copy of s
s.copy() operations (such as dict and set). copy() is not part
(same as s[:]) of the collections.abc.MutableSequence ABC, but
most concrete mutable sequence classes provide it.

extends s with the contents of t


s.extend(t)
(for the most part the same as
or s += t
s[len(s):len(s)] = t)
The value n is an integer, or an object implementing
__index__(). Zero and negative values of n clear the
updates s with its contents
s *= n sequence. Items in the sequence are not copied;
repeated n times they are referenced multiple times, as explained for
s * n under Common Sequence Operations.

inserts x into s at the index given


s.insert(i, x)
by i (same as s[i:i] = [x])

s.pop() retrieves the item at i and also The optional argument i defaults to -1, so that by
or s.pop(i) removes it from s default the last item is removed and returned.

remove the first item from s


s.remove(x) Raises ValueError when x is not found in s.
where s[i] is equal to x
Modifies the sequence in place for economy of
space when reversing a large sequence. To remind
s.reverse() reverses the items of s in place users that it operates by side effect, it does not
return the reversed sequence.

You might also like