How does Python sort user-defined objects? [duplicate] - python

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 7 years ago.
What's the default order for sorting user-defined objects in Python? I know how to change this, using the key parameter or defining the __lt__ etc, methods, but not what order you get if you don't.

Python sorts objects by default in ascending order. A simple ascending sort is done by calling the sorted() function.
It returns a new sorted list.

Related

Why does Python2 to Python3 converter put "items()" into "list"? [duplicate]

This question already has answers here:
Why does Python 3 need dict.items to be wrapped with list()?
(5 answers)
Closed 5 years ago.
I use a tool to reformat my code from Python2 to Python3. One of the things that it always does is replacing
my_dict.items()
by
list(my_dict.items())
What is the motivation behind that? I assume that items() do not guaranty the ordering (it is stochastic, since dictionary is not ordered). So, by using list we order the key, value pairs.
Is my assumption correct?
The keys(), values(), and items() methods of list in 3.x return views rather than lists, so to preserve their 2.x behavior we need to convert the view into a list.
If this old behavior is not required (e.g. we only ever iterate over the result) then the call to list() can be removed.

Is it considered better practice in Python to pass tuples instead of lists to functions? [duplicate]

This question already has answers here:
What's the difference between lists and tuples?
(22 answers)
Closed 5 years ago.
In Python is it better practice to write functions that accept tuples instead of lists if the data passed should be unmodified? Similar to how passing arguments by const reference in C++ makes it clear to the user that data won't be modified.
No, tuples and lists are different things. Tuples have strucure, lists have order. Use each for its own purpose.

return only item in a `Frozenset` [duplicate]

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.

Is it safe to assume that == operator will always work to compare dictionary in python? [duplicate]

This question already has answers here:
What does the == operator actually do on a Python dictionary?
(3 answers)
Closed 6 years ago.
What does the == operator actually do on a Python dictionary?
From the question above , == operator will compare two dictionaries keys and their corresponding values. But I see in many places in code where the keys and values are iterated to check equality of dictionaries.
I wanted to check only the equality condition on two dictionaries.
To put it simply: yes, the == operator is safe on the built-in Python dictionaries and should always work.

Test if two lists are equal [duplicate]

This question already has answers here:
Check if two unordered lists are equal [duplicate]
(8 answers)
Closed 7 years ago.
I'm trying to write tests for my Django app and I need to check many times
if 2 lists have the same objects (i.e. that every object in A is also in B and vice versa).
I read on the assertLists/Sequence/Equal etc but for what I saw if the lists
have the same objects but in a different order (A = [a,b,c], B = [b,c,a]) then it returns an error, which I don't want it to be an error because they both have the same objects.
Is there a way to check this without looping the lists?
You can use assertCountEqual in Python 3, or assertItemsEqual in Python 2.
From the Python 3 docs for assertCountEqual:
Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.
Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.
If you are staying with list() datatype then the cleanest way if your lists are not too large would be :
sorted(list_1) == sorted(list_2)
Have a look at this Question (which is the same):
Check if two unordered lists are equal

Categories

Resources