4

I have 2 lists here:

list1 = [happy, sad, grumpy, mad]
list2 = [2, 5, 6, 9]

I want to make it so that the numbers are assigned to the emotions? (happy is equal to 2, sad is equal to 5, etc). Ideally, I want to make it so you can compare items from list1, for example:

if happy > sad:
    print ("you are happy")

I want to make this code as efficient as possible, so I do not want to separately assign each variable from list1 a number.

Asocia
  • 5,935
  • 2
  • 21
  • 46
conyieie
  • 225
  • 2
  • 10
  • 2
    Had you variables happy, sad, grumpy, mad, or you mean "happy", "sad", "grumpy", "mad" in your `list1`? – MarianD May 31 '20 at 12:04

7 Answers7

8

You can zip the lists together and create a dict from them:

list1 = ["happy", "sad", "grumpy", "mad"]
list2 = [2, 5, 6, 9]

moods = dict(zip(list1, list2))
# This will create a dictionary like this
# {'happy': 2, 'sad': 5, 'grumpy': 6, 'mad': 9}


if moods["happy"] > moods["sad"]:
    print("You are happy")
else:
    print("You are sad")

The output is:

You are sad

Edit:

Another option would be directly choosing a mood if you don't care what the other values are (inspired by Laurent B. 's answer):

list1 = ["happy", "sad", "grumpy", "mad"]
list2 = [2, 5, 6, 9]

mood = list1[list2.index(max(list2))]
print("You are", mood)

Output:

You are mad
Asocia
  • 5,935
  • 2
  • 21
  • 46
3

An interesting way is to create a dict like this :

dico = {'happy':2, 'sad':5, 'grumpy':6, 'mad':9}
inv_dico = {v:k for k,v in dico.items()}

mood = inv_dico[max(dico['happy'], dico['sad'])]

print("you are : ", mood)
# you are : sad

note : you could use min instead of max to have the inverse effect

Laurent B.
  • 1,653
  • 1
  • 7
  • 16
  • 1
    A **better way** than your “Best way” is in the [Asocia's answer](https://stackoverflow.com/a/62115838/7023590). :-) – MarianD May 31 '20 at 12:21
  • I didn't say my way was the best, I said best way is to create a dict – Laurent B. May 31 '20 at 12:28
  • 1
    I actually like this answer as it can be easily modified to cover more situations. Try changing it to this: `mood = inv_dico[max(inv_dico)]` and it will give you the output: "You are mad", which is pretty cool :) – Asocia May 31 '20 at 12:32
  • thanks very much but I knew I was mad ;-) – Laurent B. May 31 '20 at 12:34
  • *“I said best way is to create a dict”* — Are you sure that you are able to recognize the best way? – MarianD May 31 '20 at 12:48
  • There was no assumption, just the fact I am not english native, "a way is to create a dict" if you want. You are right it's ambiguous, I change the words so It won't hurt anyone or narrow the creation perspective. – Laurent B. May 31 '20 at 12:53
  • Now it's OK. Thanks. – MarianD May 31 '20 at 13:07
2

You can also use index(). Dictionary version, however, is better and cleaner for the typical key-value situation.

list1 = ['happy', 'sad', 'grumpy', 'mad']
list2 = [2, 5, 6, 9]

idx = list1.index

if list2[idx('happy')] < list2[idx('sad')]:
    print('sad')
else:
    print('happy')
Gribek
  • 191
  • 6
1

You can use a dictionary in python. A simple way to represent a key-value pair. Ex: did = {'jack': 4098, 'sape': 4139} click on it for better understandinghow to use dictionary

Shivam Jain
  • 53
  • 1
  • 5
1

You can just use simple dict for data mapping

emotions = {'happy': 2, 'sad': 5,  'grumpy': 6, 'mad': 9}


if emotions['happy'] > emotions['sad']:
    print("you are happy")
else:
    print("you are sad")

1

You may use the standard enum module to accomplish it:

from enum import IntEnum

class Mood(IntEnum):
    happy = 2
    sad = 5
    grumpy = 6
    mad = 9

if Mood.happy > Mood.sad:         # Nonsense; do you mean "if yourmood < Mood.sad:"?
    print ("you are happy")
MarianD
  • 13,096
  • 12
  • 42
  • 54
0

If you don't want to use a dictionary, you can use exec function.

list1 = ['happy', 'sad', 'grumpy', 'mad']
list2 = [2, 5, 6, 9]

for L,i in zip(list1,list2):
  exec('{}={}'.format(L,i))

if happy > sad:
  print ("you are happy")

Notice here that you must use strings to name the variables (you can't have a list of unassigned variables).

jpf
  • 1,447
  • 12
  • 22