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.
Related
This question already has answers here:
What is the return value of the range() function in python?
(5 answers)
Closed 6 months ago.
On an online course, the teacher says that range() produces a tuple, adding that this is the reason why we have to convert it into a list (thanks to the list() function) if we want to modify it.
Is this statement true ?
Because in the official documentation, they dont talk about tuple at all in the range() section : https://docs.python.org/3/tutorial/controlflow.html#the-range-function
Starting with python3, range returns a range object which is a sequence type which, among other things, can be iterated to create a list out of it.
Relevant docs about the range type.
Before that, it used to return a list, like Klaus commented. (docs)
This question already has answers here:
How do I get the number of elements in a list (length of a list) in Python?
(11 answers)
Closed 4 years ago.
I use the method split() of the string class and supposedly it returns a list, but it isn't a common list because I can't use list methods like count()
I've tried to use the count method of list but it doesn't work.
def do_prueba(self, s):
lista=s.split()
len=lista.count()
print(len)
I want to know the length of that list. I'm new to Python sorry.
You aren't providing an argument to count. Just use the len function to get the length (not to be confused with the variable you named len, which you shouldn't do as it overrides the built-in):
def do_prueba(self, s):
lista=s.split()
print(len(lista))
This question already has answers here:
If in Python I put a list inside a tuple, can I safely change the contents of that list? [duplicate]
(2 answers)
Why can tuples contain mutable items?
(8 answers)
a mutable type inside an immutable container
(3 answers)
Closed 4 years ago.
I am new in Python and I need some clarity on this.
I read everywhere that tuples are immutable, so they cannot be changed, however I did this test, and I could change the values inside a list within a tuple, am I doing something wrong? Or why am I able to change this value? Thanks.
This is my test code:
>tuplelist = ('mouse',[1,2,3])
**('mouse', [1, 2, 3])**
>tuplelist[1][0]=999;
**('mouse', [999, 2, 3])**
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