3

I'd like to build something like:

A = (
  'parlament',
  'queen/king' if not country in ('england', 'sweden', …),
  'press',
  'judges'
)

Is there any way to build a tuple like that?

I tried

'queen/king' if not country in ('england', 'sweden', …) else None,
'queen/king' if not country in ('england', 'sweden', …) else tuple(),
'queen/king' if not country in ('england', 'sweden', …) else (),

but nothing is working, there doesn't seem to be an tuple-None-element, so I have a 3-tuple for all countries beside England, Sweden, etc. for which I get a 4-tuple

Hoffmann
  • 1,050
  • 9
  • 26
  • 1
    it is unlikely that a tuple is what you want, if its arity is not constant. You need either a `list`, or a 4-tuple where it always has something (e.g. `None`) in the second field. If you will explain how you intend to use it, we will be able to give you better answers. – Elazar Jun 12 '13 at 08:54
  • 1
    What you tried seems to work for me. What exactly doesn't work. Do you receive a `SyntaxError`? – TerryA Jun 12 '13 at 09:02
  • I could use a `list` but that dosn't solve my problem of looking for a "do-insert-nothing"-else case – Hoffmann Jun 12 '13 at 09:05

5 Answers5

7

Yes, but you need an else statement:

>>> country = 'australia'
>>> A = (
...   'parlament',
...   'queen/king' if not country in ('england', 'sweden') else 'default',
...   'press',
...   'judges'
...      )
>>> print A
('parlament', 'queen/king', 'press', 'judges')

Another example:

>>> country = 'england'
>>> A = (
...   'parlament',
...   'queen/king' if not country in ('england', 'sweden') else 'default',
...   'press',
...   'judges'
...    )
>>> print A
('parlament', 'default', 'press', 'judges')

This is a conditional expression, otherwise known as a ternary conditional operator.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • let A be the rouling forces in a country, how is 'default' fitting in with that? – Hoffmann Jun 12 '13 at 09:31
  • @Hoffmann It was just a string I made up. You can fit it with whatever you wish :) – TerryA Jun 12 '13 at 09:31
  • But there is not value that makes sense and the answer got so many votes I think it is misleading. (Plus I'd remove the downvote but I can't as I also think it is to harsh) – Hoffmann Jun 12 '13 at 09:42
  • @Hoffmann I'll edit the question so you can remove it. The upvotes were given because it answered your question (and that they seemed to like it too). Would you like me to change the string too :)? – TerryA Jun 12 '13 at 09:44
  • Yeah, please change it to 'spaghettiMonster' as that mit be a rouling force as well ;-) - but yeah, you are right, I don't need to try to "correct" the decision of others – Hoffmann Jun 12 '13 at 16:00
  • The answer does not solve OP's problem, and is somewhat misleading in only printing the non-default case. OP asks for a way to avoid a default, instead making a tuple without the conditional element when the test fails. (At least in the most current version of the question.) – Elias Hasle May 06 '19 at 18:47
5

can propose You following

A = (('parlament',) +
     (('queen/king',) if not country in ('england', 'sweden', …) else tuple()) +
     ('press', 'judges'))

this allows You to include or not include elements in result tuple (unlike default value, which will be included if You will not use tuple concatenation.

A = ('parlament',
     'queen/king' if not country in ('england', 'sweden', …) else 'default',
     'press', 'judges')
oleg
  • 4,082
  • 16
  • 16
  • 1
    for anyone using this sollution, take care not to forget the `,` in the tuple otherwise it will blow – Hoffmann Jun 12 '13 at 09:25
2

I ran into a similar problem. You can use the spread operator *:

A = (
  'parlament',
  *(('queen/king',) if not country in ('england', 'sweden', …) else tuple()),
  'press',
  'judges'
)

Looks a little bit complicated but does exactly what is requested. First it "packs" the whatever answer is needed into a tuple (resulting either in an empty tuple or in a single–element tuple). Then, it "unpacks" the resulting tuple and merges it into the right place in the main outer tuple

1

Yes you can, but for that your ternary condition must be a valid one, i.e you require an else clause too.

Ternary operator in python:

>>> 'x' if False else 'y'
'y'

Your code:

A = (
  'parlament',
  'queen/king' if not country in ('england', 'sweden') else 'foo',
  'press',
  'judges'
   )
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

You can use Ternary conditional operator eg:

A= ('a', 'b', 'c' if condition else 'd')
Jisson
  • 3,566
  • 8
  • 38
  • 71