Python: Pick other value

Please excuse (or improve the title) but I have a silly little problem that's making me very unsure.

I have a list that can contain one to two values, never more, never less and only these two options:

options = ['option one', 'option two']

As I say, sometimes there may only be one of those values in the list so it might just be ['option two',]

The scope for this is simple navigation on a website. I accept a querystring entrance and search the list for those options:

current_option = request.GET.get('option', options[0]) if not current_option in options: current_option = options[0]

If no "option" is provided, it defaults to the first available option.

But now I want to know what the other option is. If "option one" is the input, I want "option two". And if there is only "option one" in the options list, for example, I want the return to be False.

I know I can loop through the list but it feels like there should be a better way of just picking the other value.

-------------Problems Reply------------

return options[0]

Edit: If you don't want to modify options, you can also use the somewhat less readable

return (options + [False])[current_option == options[0]]

current_option = request.GET.get('option', options[0])
if not current_option in options:
current_option = options[0]
oindex = options.index(current_option)
other_option = False if len(options) != 2 else options[(oindex+1) % 2]

Category:python Views:0 Time:2011-05-16
Tags: python list

Related post

  • Which maximum does Python pick in the case of a tie? 2011-07-21

    When using the max() function in Python to find the maximum value in a list (or tuple, dict etc.) and there is a tie for maximum value, which one does Python pick? Is it random? This is relevant if, for instance, one has a list of tuples and one sele

  • How do I make Python pick the correct module without manually modifying sys.path? 2009-11-05

    I have made some changes in a python module in my checked out copy of a repository, and need to test them. However, when I try to run a script that uses the module, it keeps importing the module from the trunk of the repository, which is of no use to

  • Python: pick appropriate datatype size (int) automatically 2011-12-19

    I'm allocating a (possibly large) matrix of zeros with Python and numpy. I plan to put unsigned integers from 1 to N in it. N is quite variable: could easily range from 1 all the way up to a million, perhaps even more. I know N prior to matrix initia

  • How to change version of Python picked up by Cygwin 2012-03-07

    I have two versions of python installed on Win7. (Python 2.5 and Python 2.7). These are located in 'C:/Python25' and 'C:/Python27' respectively. I am trying to run a file using Python 2.5 but by default Cygwin picks up 2.7. How do I change which vers

  • Python: Picking an element without replacement 2010-04-06

    I would like to slice random letters from a string. Given s="howdy" I would like to pick elements from 's' without replacement but keep the index number. For example >>> random.sample(s,len(s)) ['w', 'h', 'o', 'd', 'y'] is close to what I wa

  • The last line of this python program uses both " and ' but I don't know why 2011-08-13

    Ok on this link it shows the last line of output that has ' around everything except the third sentence and I do not know why. This bothered me at the beginning and thought it was just a weird mistake but its on the "extra credit" so now I am even mo

  • python - audio classification of equal length samples / 'vocoder' thingy 2011-11-29

    Anybody able to supply links, advice, or other forms of help to the following? Objective - use python to classify 10-second audio samples so that I afterwards can speak into a microphone and have python pick out and play snippets (faded together) of

  • How difficult is it to learn functional programming languages? 2009-11-15

    Given that a persona has extensive experience in different imperative programming languages right from C to JAVA. Whats the learning curve for functional programming languages such a Haskell ? --------------Solutions------------- Taken from link User

  • Accessing py2exe program over network in Windows 98 throws ImportErrors 2010-01-05

    I'm running a py2exe-compiled python program from one server machine on a number of client machines (mapped to a network drive on every machine, say W:). For Windows XP and later machines, have so far had zero problems with Python picking up W:\pytho

  • iterating through a list removing items, some items are not removed 2010-03-29

    I'm trying to transfer the contents of one list to another, but it's not working and I don't know why not. My code looks like this: list1 = [1, 2, 3, 4, 5, 6] list2 = [] for item in list1: list2.append(item) list1.remove(item) But if I run it my outp

  • Is it possible for a Chrome extension to write to a file in Chrome's system directory? 2011-12-03

    I've never really written a real Chrome extension. I did one that was just a dropdown list of links a while back, but that doesn't really count. Anyway, rather than dive head first back into the massive collection of tutorials on writing Chrome exten

  • On Heroku, locale.getdefaultlocale() is returning (None, None), breaking Django createsuperuser – how to fix? 2012-01-24

    Trying to do a heroku python createsuperuser gave me an error that ended roughly: File "/usr/local/www/site-python/lib/django-trunk/django/contrib/auth/management/", line 85, in get_system_username return getpass.getuser().decode

  • Reverse-enginering communication protocol 2012-02-18

    What serialization format is this, and are there any libraries to parse it back to python-native data structures or at least something easier to manage? At least it looks like it could have a 1:1 correspondent in python. %xt%tableFameUpdate%-1%{"seas

  • Python code to pick out all possible combinations from a list? 2009-01-21

    I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. I've found some code (by googling) that apparently does what I'm looking for, but I found the code fairly opaque and am wary of us

  • Picking out items from a python list which have specific indexes 2009-04-07

    I'm sure there's a nice way to do this in Python, but I'm pretty new to the language, so forgive me if this is an easy one! I have a list, and I'd like to pick out certain values from that list. The values I want to pick out are the ones whose indexe

  • What's the quickest way for a Ruby programmer to pick up Python? 2009-05-10

    I've been programming Ruby pretty extensively for the past four years or so, and I'm extremely comfortable with the language. For no particular reason, I've decided to learn some Python this week. Is there a specific book, tutorial, or reference that

  • How do I pick 2 random items from a Python set? 2009-08-11

    I currently have a Python set of n size where n >= 0. Is there a quick 1 or 2 lines Python solution to do it? For example, the set will look like: fruits = set(['apple', 'orange', 'watermelon', 'grape']) The goal is to pick 2 random items from the

  • Automatically pick tags from context using Python 2009-11-21

    How can I pick tags from an article or a user's post using Python? Is the following method ok? Build a list of word frequency from the text and sort them. Remove some common words and pick the top 10 words remained in the list as the tags. If the abo

  • Python vs. Java - Which would you pick to do concurrent programming and why? 2009-12-07

    Also, if not python or java, then would you more generally pick a statically-typed language or a dynamic-type language? --------------Solutions------------- I would choose the JVM over python, primarily because multi-threading in Python is impeded by

Copyright (C), All Rights Reserved.

processed in 0.068 (s). 11 q(s)