#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Getting the name of a list if element exists within it.


    Hi,

    Say I have 3 lists that have unique elements. Is there a way I can search for an element within the 3 lists and if it exists within one of them, then print out the list name?

    So if I have lists

    fruits = ['bananas', 'apples', 'oranges']
    breads = ['french', 'garlic', 'wheat']
    cheeses = ['swiss', 'provolone', 'cheddar']


    Is there a way I could search for say 'wheat' and get the list name (bread) returned. I know that I can use a bunch of if/elif statements and check whether 'wheat' is in a specific list, but I wanted to see if there was a shorter and perhaps more efficient way of doing this.
    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    While there is probably some low-level hack for printing out the actual variable name of a variable, it's not really the way you want to write code. Using variable names as data is not good practice.

    A better way to approach this is to put your variables in a dict, like so:

    Code:
    foods = {
        "fruits" : ['bananas', 'apples', 'oranges']
        "breads" : ['french', 'garlic', 'wheat']
        "cheeses" : ['swiss', 'provolone', 'cheddar']
        }
    You can then iterate through the keys of the dictionary, and use the "in" keyword to see if your variable is in the list, like so:

    Code:
    for category, foodlist in foods.items():
        if myfood in foodlist:
            print(category)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by admoore
    While there is probably some low-level hack for printing out the actual variable name of a variable, it's not really the way you want to write code. Using variable names as data is not good practice.

    A better way to approach this is to put your variables in a dict, like so:

    Code:
    foods = {
        "fruits" : ['bananas', 'apples', 'oranges']
        "breads" : ['french', 'garlic', 'wheat']
        "cheeses" : ['swiss', 'provolone', 'cheddar']
        }
    You can then iterate through the keys of the dictionary, and use the "in" keyword to see if your variable is in the list, like so:

    Code:
    for category, foodlist in foods.items():
        if myfood in foodlist:
            print(category)
    Thank you for that. I guess I wasn't thinking straight all the way through when I asked the question. I'll change my implementation from a list to a dictionary.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    You'll be changing from several variables each holding a list to one dictionary, the value of each key is a list.

    Use sets if you need to use large searches.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo