Recent Posts

python mutable sequence operator



Hello I am Juny.

we learned common sequence type operators in the operators of python sequence type like list, tuple and range.

we will learn mutable sequence type operators in this post.
typically,
there are list and string in mutable sequence type.


mutable sequence type operators


in below table,
s is sequence, t is the other sequence and x is any object.




operatoroperation
s[i] = xitem i of s is replaced by x
s[i:j] = tslice of s from i to j is replaced by the contents of the iterable t
del s[i:j]same as s[i:j] = []
s[i:j:k] = tthe elements of s[i:j:k] are replaced by those of t
del s[i:j:k]removes the elements of s[i:j:k] from the list
s.append(x)appends x to the end of the sequence (same as s[len(s):len(s)] = [x])
s.clear()removes all items from s (same as del s[:])
s.copy()creates a shallow copy of s (same as s[:])
s.extend(t) or s += textends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)
s *= nupdates s with its contents repeated n times
s.insert(i, x)inserts x into s at the index given by i (same as s[i:i] = [x])
s.pop([i])retrieves the item at i and also removes it from s
s.remove(x)remove the first item from s where s[i] is equal to x



for better understanding,
let's check them in code.


s = [1,2,3]
t = "abc"
x = 4

s[0] = x
print("s[0] = x = ", s)

s[0:2] = t
print("s[0:2] = t = ", s)

del s[0:3]
print("del s[0:3] = ", s)

s = [1,2,3,4,5]
s[0:5:2] = t
print("s[0:5:2] = t =", s)

del s[0:5:2]
print("del s[0:5:2] =", s)



python mutable sequence operators 1



x is assigned to 0th of s with []
as the result, 0th of s comes to be 4 that is x's value.

s is sliced from 0th to the first with [:].
and, that is replaced to t.
as the result, [4,2] of s comes to be ['a','b','c'].

part of s from 0th to the second is removed with del [:].
as the result, ['a','b','c'] is removed from s and only [3] is left.

part of s from 0th to 5th by 2 steps is replaced to t.
[1,3,5] that is sliced with [0:5:2] is replaced to [a,b,c].
as the result, [1,2,3,4,5] comes to be ['a',2,'b',4,'c'].

the length of the part to be replaced must same to the length of part to replace it.

the part of s from 0th to 5th by 2 steps is removed with del [::].
as the result, ['a','b','c'] is removed from ['a',2,'b',4,'c'] and then [2,4] is left.

we will continue to learn the rest of list.


s = [1,2,3]
t = "abc"
x = 4

s.append(x)
print("s.append(x) = ",s)

s.clear()
print("s.clear() = ",s)

s = [1,2,3]
b = s.copy()
print("b = s.copy() = ",b)


s.extend(t)
print("s.extend(t) = ",s)

t*=2
print("t*=2 = ",t)

s.insert(0,x)
print("s.insert(0,x) = ",s)

print("s.pop(0) = ", s.pop(0))
print("s = ",s)

s.remove(3)
print("s.remove(3) = ",s)


python mutable sequence operators 2


"append" will append x to the end of s.

"clear" will clear all the items of s.

"copy" will copy s.

"extend" will concatenate t to s.

s will repeat n times with "*".

"insert" will insert x to 0th of s.

data that is located in 0th of s returns. and then will be removed from s.

"remove" will find the data that is parameter of "remove" and remove it.
if there are multiple data, the first will be removed.


python mutable sequence operator python mutable sequence operator Reviewed by Juny on 7월 27, 2019 Rating: 5

댓글 없음:

Powered by Blogger.