-1

I make a Python script that searches for the specific things in a list (e.g. animals or flower).

Let's suppose I have variables such as:

dog = 1
cat = 1
rose = 0
violet = 0

And a list of those things

List_of_things = [dog, cat, rose, violet]

How do I make it so the print function only displays those members of a list that have a value of 0 or 1 (only animals or flowers)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 5
    Your list is simply `[1, 1, 0, 0]`. It doesn't know what the original variable names were. – interjay Jul 30 '23 at 16:22
  • 1
    Helpful resource for understanding [Python names and values](https://nedbatchelder.com/text/names1.html). – jarmod Jul 30 '23 at 18:21

3 Answers3

3

About variables, evaluation and list creations

The following Python statements:

dog = 1
cat = 1
rose = 0
violet = 0

Will create variables named dog, cat, rose and violet with the values:

  • 1 for the variable dog
  • 1 for the variable cat
  • 0 for the variable rose
  • 0 for the variable violet

The type of all of these values are going to be integer (or int)

The following python statement:

List_of_things = [dog, cat, rose, violet]

Will create a variable called List_of_things and assign it the value of the expression in the right side of the = operator.

To assign the value of the expression in the right side of the = operator, it first has the evaluate the expression. The expression is the following:

[dog, cat, rose, violet]

This expression means, because of the [ and ] symbols, that you want a new list to be created from the values provided. But in our case the values provided are variables, which means they must be evaluated (in the case of a variable, converted to the value they hold) before they are provided as an item for the list to be created.

Therefore, the expression to be evaluated is equivalent to:

[1, 1, 0, 0]

And the list to be created won't store the information about which variables were used to create the list.

The expression then will be evaluated to the list object created from the provided values.

Solution with dict

One possible way of achieving the desired effect would be to use a dict to associate each possible string (value) to be printed to an integer.

integer_associated_to_string = {
    "dog" : 1,
    "cat" : 1,
    "rose" : 0,
    "violet" : 0
}

Then you could iterate over the entries of the dict and test for a condition:

for item in integer_associated_to_string:
    if integer_associated_to_string[item] in [0,1]:
        print(item)

Note in this case item will iterate over the key values of the dict (the values to the left of the : during the dict creation), and integer_associated_to_string[item] will evaluate to the value on the right of the : during the dict creation.

Solution with list of tuple

One limitation of the dict approach would be that dict keys must be unique, in this case a list of tuples would allow for repetition of string values in the list.

For example, for a new category associated with integer 2 meaning colors:

list_of_tuple = [
("dog", 1),
("cat", 1),
("rose", 0),
("violet", 0),
("violet", 2)
]

violet could also be a color (category 2), and dict wouldn't allow for this association.

To iterate in this case:

for item in list_of_tuple:
    if item[1] in [1,2]:
        print(item[0])

Here item[1] means the integer category and item[0] means the string itself.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • OMG delta de dirac thank you for your detailed answer. you made everything clear to me ! thank you for your time and effort that you have put in. YOU ARE AWESOME <3 – user22309639 Jul 30 '23 at 17:22
2

It's generally not a good idea to reference variable names in your desired output. It can be done, but it gets ugly very quickly.

Instead I would opt for a dictionary, where you can keep the animal names as strings and store their values alongside them. Afterwards, you can use filter() to filter the keys of this dictionary based on the values.

animals = {
    'dog': 1,
    'cat': 1,
    'rose': 0,
    'violet': 0
}

desired_value = 1

for animal, value in animals.items():
    if value == desired_value:
        print(animal)

# OR

print(*(animal for animal, value in animals.items() if value == desired_value))

Output:

dog
cat

# OR

dog cat
B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11
  • I think OP meant "either show elements that have a value of 1, or that have a value of 0". Also that last line would be _much_ simpler as a list comprehension or just a generator: `print(*(x for x in animals if animals[x] == 1))` – tobias_k Jul 30 '23 at 16:57
  • Good point. That solution is much simpler, I got lost in trying to show that the initial idea would be a mess. – B Remmelzwaal Jul 30 '23 at 17:02
0

In your case, I think you should use a dictionary like this if you want to print the name like dog, cat, ...:

# It is a dictionary. It is a like a list, but the indexes are customized.
dict_of_things = {
    'dog' : 1,
    'cat' : 1,
    'boat' : 2000,
    'rose' : 0,
    'violet' : 0,
    'car' : 12
}


# This list (dict_of_things.keys()) will done ['dog', 'cat', 'boat', 'rose', 'violet', 'car']

# The .keys() is to get all the keys (names like dog, cat, etc.) of a dictionary.

# And the value keys of the 'for' will take 'dog' in the first run, after 'car', etc.

for keys in list(dict_of_things.keys()):

    # First run of the for will be:
    #   if 'dog' value (1) is in the list [0, 1]
    if(dict_of_things[keys] in [0, 1]):

        # First run:
        #print('dog')
        print(keys)

Output:

dog
cat
rose
violet
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Meniev
  • 148
  • 1
  • 13