Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 comparing multiple values
#1
i have several variables that at one point i expect them all to have the same value and need to test it:
   ...
   if a==b and b==c and c==d and d==e and e==f and f==g and g==h:
        print('they are all the same')
    else:
        print('it did not work')
is there a better way to test if they are all alike?  i can set it up so they are all in a list or tuple making up the whole sequence.  i'm thinking maybe sorting a list and comparing first to last.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
First thing that comes to mind is doing it with a for loop.

But a nice thing to try would be putting all elements you want to check in a set and then checking its length, since there will be no duplicate items.
Doing it that way is also practical, because you can keep using/modifying the same set throughout the program, if it is a long running/repetitive task.
Quote
#3
Yes, just use functional programming style:

functools.reduce(operator.eq, [a,b,c,d])
micseydel likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#4
You can chain them:

>>> a, b, c, d = 1, 1, 1, 1
>>> a == b == c == d
True
>>>
But I like the @DeaD_EyE's answer more. I have to see this reduce and operator methods.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#5
@DeaD_EyE's answer may well be the more elegant answer.  but is it the more pythonic one?  is it the better one?  now that you have seen them all, which would you code for this case?  are there any you would be upset with if you see them in someone else's code?  are there any you would be upset with if they are in contributions to one of your projects?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#6
You can always put some comment to clarify what is doing. @DeaD_EyE's answer can be applied to a list with arbitrary length without getting ugly.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to pass multiple values from one sample to nc variable? Baloch 0 96 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Inserting values from multiple lists sqlite azulu 1 257 May-24-2020, 08:40 AM
Last Post: ibreeden
  UnUnloading values from multiple widgets in a container UGuntupalli 3 249 Apr-20-2020, 08:53 PM
Last Post: UGuntupalli
  Save all values to pandas of multiple classes jenniferruurs 0 260 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 522 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Try for multiple values bluethundr 1 574 Mar-07-2019, 09:04 PM
Last Post: bluethundr
  Comparing values in large txt files StevenVF 2 531 Feb-28-2019, 09:07 AM
Last Post: StevenVF
  Get multiple values from function and total it. nirlep 5 785 Feb-05-2019, 06:54 AM
Last Post: nirlep
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 978 Jun-22-2018, 03:08 PM
Last Post: wavic
  Comparing values in separate lists KaleBosRatjes 3 1,018 May-02-2018, 04:38 PM
Last Post: KaleBosRatjes

Forum Jump:


Users browsing this thread: 1 Guest(s)