This question already has answers here:
Difference between del, remove, and pop on lists
(14 answers)
Closed 5 years ago.
Is my understanding correct?
pop() remove and return the last item in the list, so it can be used to implement “First In Last Out” structure
by pop(), and “First In First Out” structure by pop(0)
del can also be used to delete specified index’s item.
So, my question is what’s the difference between those two. If those two are the same,
why would Python creator bother to create both? Is there only difference that pop() returns the
remove item while del does not?
You're correct that, for single list elements, the value or lack thereof is the only difference. However, del has a broader range of applicability: it can remove slices from a list, and it can also destroy variables, attributes, and mapping entries.
Related
This question already has answers here:
Python - append VS extend efficiency
(3 answers)
Closed last year.
For example:
lst.append(x)
lst.append(25)
lst.append(y)
Would it be better to write this:
lst.extend([x, 25, y])
I would argue that it depends on the nature of the list being appended. Given that the list is fixed, meaning that you are sure what elements are in it, it would make sense to use the latter version (lst.extend([x, 25, y])).
Given that you want to append elements depending on certain logical conclusions, you should probably append elements separately, except if you are sure that two elements are always being appended together.
What I mean by this is that if you want to append elements to the list depending on logic such as if-statements, where one if statement might append one element to the list but not another, it makes sense to split the elements being appended into several separate elements. If you know on the other hand that all elements will be added at the same time, you would much rather use the latter version, as it makes your code more compact and easier to read.
extend() also runs faster than append(), if that is what you are after
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 2 years ago.
Just a philosophical question, I'm a rookie python programmer and I really like it, my question is, in any data type that uses the copy function (i.e. Lists, tuples, sets, etc.), why to use it instead of using the Assignment operator (=) like :
List_new = List_Old
Instead of writing:
List_new = List_Old.copy()
Literally, Why???
Well if use assignment operator ‘=‘ to assign the list to new list it doesn’t make a copy but now both of them point to the same list, any changes you make to the new list will be reflected in the original list also i.e if you add an element to the new list it will also be added to original list and same happens if you remove list elements. But if you use copy() function to copy a list to a new list any changes you make in the new list won’t be reflected in the original list.
This question already has answers here:
Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?
(6 answers)
Closed 5 years ago.
I am a beginner to programming and was working in a for loop and realised the error I was getting is because of the reason in the title. Why Does the happen anyway? Is this a general property of mutable ojects?.
T
append modifies the list in place. There is no need to do L=L.append(x); Simply running L.append(x) should suffice. The reason L is being set to none in your code is because there is no return value for append
This question already has answers here:
Access the sole element of a set [duplicate]
(6 answers)
How to get an arbitrary element from a frozenset?
(4 answers)
Closed 5 years ago.
I have a situation in my code where there is a Frozenset which contains a single number (e.g. Frozenset([5])). What I want to do is get that value into a variable. What is the pythonic way to do this?
Since you can iterate over a Frozenset, I have already tried to do it like this: var = next(myFrozenSet) but it does not work, since a Frozenset is not actually an iterator.
I also tried to use myFrozenSet.pop(), but this is not attribute of Frozensets.
You can create an iterator with the iter() function:
element = next(iter(some_frozen_set))
This is the most efficient method of getting a single element out of a frozenset; all other methods involve creating another container first (like a set or list), which is more costly than creating an iterator.
This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 6 years ago.
x=([1,2,3])
type(x)= List
x=([1,2,3],[4,5,6])
type(x)=tuple
why does the type change?
The proper syntax for creating a tuple with only one item is to follow the item with a comma:
x=([1,2,3],)
which for this example will in fact give
type(x)=tuple
Reference the official Python 2 documentation
which states (quote)
A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).