Python Forum
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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.
Reply
#3
Yes, just use functional programming style:

functools.reduce(operator.eq, [a,b,c,d])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
Reply
#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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 1,898 Jan-03-2024, 09:42 AM
Last Post: buran
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  How to combine multiple column values into 1? cubangt 15 2,634 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Function - Return multiple values tester_V 10 4,321 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,442 Mar-09-2021, 12:24 PM
Last Post: KMV
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,847 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Assigning multiple values using tuple sivacg 2 2,226 Aug-06-2020, 10:29 PM
Last Post: perfringo
  Comparing Values/QC Within Two Strings uttadms31 2 1,877 Jul-07-2020, 03:49 PM
Last Post: uttadms31
  How to pass multiple values from one sample to nc variable? Baloch 0 1,838 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Inserting values from multiple lists sqlite azulu 1 2,453 May-24-2020, 08:40 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020