This question already has answers here:
Why does del (x) with parentheses around the variable name work?
(2 answers)
"assert" statement with or without parentheses
(6 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
>>> li = [1, 2, 3, 4]
>>> li
[1, 2, 3, 4]
>>> del li[2] #case 1
>>> li
[1, 2, 4]
>>> del(li[2]) # case 2
>>> li
[1, 2]
>>> del (li[1]) # case 3
>>> li
[1]
>>>
One of my professors used case 2 to delete item from list.
As per python documentation case 1 is right and there is also another syntactic way exist from this answer so case 3 also right, but as per my knowledge there is no del method exist in python, how case 2 is valid. I searched whole python documentation but could not find it.
Update:
if i write del method myself in my module and use case 2 at same time, how python interpreter differentiates between them or will it through an error, although i never tried until now
All of them are the same, del is a keyword as yield or return, and (list[1]) evaluates to list[1]. So del(list[1]) and del (list[1]) are the same. For the base case, since you dont have the () you need to force the extra space, hence del list[1].
EDIT: You cannot redifine del since it is a language keyword.
The parenthehis is not mandatory with keyword (like if or del), but can put some if you want.
it's exactly the same thing
Related
This question already has answers here:
Why doesn't .strip() remove whitespaces? [duplicate]
(3 answers)
Closed 1 year ago.
I want to learn what is the difference between two code lines.I couldn't find the difference.Whenever I try to run the second code,it doesn't affect string a.
Could someone tell me why the second code line doens't work?
a = "aaaaIstanbulaaaa".strip('a') #Affects the string
print(a)
>>>Istanbul
a = "aaaaIstanbulaaaa" #Doesn't affect the string
a.strip('a')
print(a)
>>>aaaaIstanbulaaaa
str.strip returns a value; it does not modify the str value invoking it. str values are immutable; you cannot modify an existing str value in any way.
The str.strip() isn't an inplace method, it doesn't change the current object you're calling with, it returns a new one modified
That is an example of inplace modification
x = [1, 2, 3]
x.append(4)
# x is [1, 2, 3, 4]
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
What is the length of d? d = [1, 2, 3][1:]. I can't understand what the [1:] mean at the end.
It’s referred to as ‘slicing’. Examples shown here.
From element 1 (the second element) through to the end, therefore a length of 2.
Aside: You’d (hopefully) never see this exact case in real-world practice, as it’s nonsense.
This question already has answers here:
What is the __lt__ actually doing for lists [duplicate]
(2 answers)
Closed 4 years ago.
How come max([1,2,3], [1,1,4]) returns [1,2,3] not [1,1,4]?
I was asked this question in a class. I don't understand why it returns [1,2,3] and the logic behind it (even if it returns [1,1,4], I still don't understand what max() function does).
The function max will succeed in outputing a maximum as long as the provided arguments are comparable.
In this case, the first argument is greater than the second with regard to list-ordering.
>>> [1, 2, 3] > [1, 1, 4]
True
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 7 years ago.
So basically i've observed that in python3 if i do something like
a = 5
def addFive(x):
x+=5
return x
print(addFive(a),a)
The output will be "10 5" since a is not changed by the function.
However the same does not happen for lists:
a = [1,2,3]
def b(x):
z = x
z.append(10)
b(a)
print(a)
When i run the function it changes the actual list.
Now to my Question: Why does this happen, where can i read up more about this(Honestly have no idea how to word my google search) and how can i avoid this. Please feel free to redirect me to other articles as this could be a common problem but I honestly couldn't find anything similar.
Thanks in advance :)
Use the copy module.
import copy
a = [1,2,3]
def b(x):
z = copy.deepcopy(x)
z.append(10)
return z
b(a)
print(a)
This prints
[1, 2, 3, 10]
[1, 2, 3]
Each element in a Python list is a location in memory, so when you modify an element, you're actually modifying that reference in memory. So if you're familiar with C++, passing a list into a function in Python, is similar in concept to passing by reference.
This question already has answers here:
Can I create a "view" on a Python list?
(10 answers)
Closed 8 years ago.
Is there a way to create a "slice view" of a sequence in Python 3 that behaves like a regular slice but does not create a copy of the sliced part of the sequence? When the original sequence is updated, the "slice view" should reflect the update.
>>> l = list(range(100))
>>> s = Slice(l, 1, 50, 3) # Should behave like l[1:50:3]
>>> s[1]
4
>>> l[4] = 'foo'
>>> s[1] # Should reflect the updated value
'foo'
I can write my own Slice class that does this but I wanted to find out if there was a built-in way.
Use islice from itertools library
EDIT:
I see where I misunderstood the question.
Well, there is no such thing. If you want to create your class, you'll have to:
Keep a reference to the original list in you Slice class
Define, __iter__, __getitem__ and __setitem__ methods to work on the original list with index conversion