1

I am trying to take value from this list (tuple in list) below :

list =  [('look', 38),
         ('ganesh', 35),
         ('said', 30),
         ('tiger', 30),
         ('cat', 28)]

What I want to do is to take value with inputting word name, for instance if I input 'look' then it will return 38 instead of inputting list[0][1] which I have already done. I have already eliminated all duplicated words in this list. Otherwise, should I remake dictionary with separating items and values?

TIshow
  • 918
  • 4
  • 12
  • 27
  • 2
    It looks like you want a dictionary. – Willem Van Onsem Feb 13 '18 at 23:53
  • 2
    past your list into the `dict` constructor: `d = dict(l)`. Note I renamed your list `l` because `list` is a type and you shouldn't use it as the name of a variable. – pault Feb 13 '18 at 23:54
  • How can I make it from this list ? – TIshow Feb 13 '18 at 23:54
  • 1
    No, you cannot make a list of tuples *act* like a dictionary without implementing, essentially, a dictionary. Instead, make your `list` of `tuple` s *into* a `dict`, i.e. `mydict = list(list_of_tuples)` – juanpa.arrivillaga Feb 13 '18 at 23:55
  • oh it was super easy...Thank you very much. – TIshow Feb 13 '18 at 23:56
  • Looks like you want a [dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). You can easily construct this from your already existing data structure. Simply pass `list` into the dictionary constructor: `dict(list)` – Christian Dean Feb 13 '18 at 23:57
  • Hy, you're right .. if you want to access 38 by "look" use a dict object list = { 'look':38, .. } list["look"] => 38 or if you cannot change your structure search = "look" for i in list: if list[0] == search: found = list[1] break – Emmanuel BRUNET Feb 14 '18 at 00:02

1 Answers1

2

If the keys have no duplicates (if these have than the problem is rather ambiguous anyway), and the keys are hashable (strings are hashable), we can use a dictionary:

thelist = [('look', 38),
           ('ganesh', 35),
           ('said', 30),
           ('tiger', 30),
           ('cat', 28)]
thedict = dict(thelist)

Now we can perform a lookup with:

thedict['look']

This will give us 38, in case we lookup a key that is not present, like 'bobcat', then Python will raise a key error:

>>> thedict['bobcat']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'bobcat

We can thus handle it with:

ourkey = 'bobcat'
try:
    ourvalue = thedict[ourkey]
except KeyError:
    # do something in case not present
    pass

Note: do not use variable names like list, dict, tuple, etc. since you override references to the list class.

Note: in case you want to use a default value instead when the lookup fails, you can use thedict.get(ourkey, defaultvalue), or in case that should be None, we can use thedict.get(ourkey). Note that if such function returns None, that does not per se means that the key was not found: it can mean that the key was not found, but it can also be the result of the fact that the key was associated with a None value.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • One can also use the `get()` method of dictionary to safely return a sensible value if the key does not exist. – pault Feb 13 '18 at 23:57
  • If `ourvalue` will simply be assigned another value in the `except` block, one could use `dict.get` (to return a default value). – Christian Dean Feb 13 '18 at 23:57
  • @pault: yes, but my experience as a developer of production software is that it frequently does more harm than good, since one of the credos of Python is *you should never pass errors silently, unless these are silenced explicitly*. I've unfortunately seen a lot of trouble arising from `.get(..)` this is partly because if you do not pass an explicit default value, then it returns `None`, but `None` can be in the dictionary as well. – Willem Van Onsem Feb 13 '18 at 23:59
  • Thank you for answers, and yes I have already removed all duplicated keys. – TIshow Feb 14 '18 at 00:01
  • Fair point- I only suggested it for the sake of completeness. – pault Feb 14 '18 at 00:37