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.
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
I am having trouble creating a function since I want to be able to refer to the tuple and not the list which contains the tuples. Hence I have come to the conclusion that I want to get rid of the inner square brackets.
I have a list similar to this:
List=[[(1,2),(3,4),(5,6)],[(1,2),(5,7),(3,8)],[...],[...]]
So the question I am asking is how can I remove the inner [ ] so that I can just produce a single list of tuples.
Also, I am not sure if I am allowed to ask another question, but how would i also delete duplicates (x,y) entries in my new list?
I have not provided code for this since I know the problem for the code I have and I believe I would confuse people by including it. If however, you wish to see the code, or want me to clarify anything please let me know.
I think this has been asked and answered on here multiple times. The solution to the flattening problem would be as follows:
new_list = [tupl for l in List for tupl in l]
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.
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:
What's the difference between lists enclosed by square brackets and parentheses in Python?
(7 answers)
Closed 6 years ago.
So my question is exactly defined in the title. That is, what is the difference between
[(a,b),(c,d)]
and
[[a,b],[c,d]]
and how I can turn one into another in Python.
This question is partly duplicate but the main part is how to turn one into another which in not duplicate.
A list is that which encloses its contents in a bracket [].
A tuple is that which encloses its contents in a parentheses ().
Lists are mutable (changeable); tuples are not.
Turning a list into a tuple is possible:
Changing a tuple to list:
list(myTuple)
Changing list to tuple:
tuple(myList)