-2

So I have a dictionary which is like -

 {'gaining': 34, 'Tinga': 42, 'small': 39, 'legs,': 13,}. 

Is there a way in which i can print it out so that it becomes a list like -

 [ gaining, Tinga, small, legs ] 

So that only the keys are printed and not the values that go along it. Also is there a way to make the dictionary not work in arbitrary order - such that if two keys are repeated instead of giving it the value of the last one, we give it the value of the first one?

eg;

   {'gaining' : 34, 'Tinga' : 42, 'small : 39, 'legs,' : 13 'gaining' : 20}

When printed

   print dict['gaining']

The output comes as

   34 

instead of coming as

   20
The World In 5
  • 71
  • 1
  • 2
  • 7
  • 2
    Dictionaries can't have duplicate keys. – Andy Jan 20 '17 at 06:26
  • @Andy Is there no way at all around it? – The World In 5 Jan 20 '17 at 06:27
  • The keys are available as a list as `.keys()` – Stephen Rauch Jan 20 '17 at 06:28
  • The last key definition will overwrite the previous definition. It seems that you are using the wrong data type for your case. – Alex Fung Jan 20 '17 at 06:28
  • @StephenRauch Is there a way around the duplicate key problem? – The World In 5 Jan 20 '17 at 06:30
  • You can use a list of tuples (or lists) instead. Then you can have duplicates. – DYZ Jan 20 '17 at 06:30
  • 2
    Duplicate key is not a problem. It is just the way dictionaries work. – Stephen Rauch Jan 20 '17 at 06:30
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Jan 20 '17 at 06:33

4 Answers4

0

dict.keys() would suffice to achieve the first thing you asked.

Your second question is a bit tricky. You see, dictionaries store key value pairs, and there is this notion that a dictionary has unique keys. So, you can not put multiple values corresponding to a key in the manner you described.

Instead, what you can do is defining each value in the dict to be a list, and appending new values for a key to the list for that key. (i.e. dict[key]) Then, when you try to read a random value of a key, you can pick a random value from the list dict[key].

kouty
  • 320
  • 8
  • 17
ilim
  • 4,477
  • 7
  • 27
  • 46
0

Dictionaries can't have two identical keys by definition. You should read the python docs to understand how they really work.

To answer your question:

d = {'gaining': 34, 'Tinga': 42, 'small': 39, 'legs': 13}
d.keys()

which will return

dict_keys(['legs', 'gaining', 'Tinga', 'small'])

If you really want to print them in the exact format you specified:

print('[' + ', '.join(d.keys()) + ']')

which will return

[legs, gaining, Tinga, small]

Note that dictionaries are unsorted by definition, so the exact output may vary.

airstrike
  • 2,270
  • 1
  • 25
  • 26
0

Printing the keys is straightforward. In Python 3:

d = {'gaining': 34, 'Tinga': 42, 'small': 39, 'legs,': 13,}
print(list(d.keys()))

In Python 2, dict.keys already returns a list instead of a special view object, so you can do

print d.keys()

You can set values in a dict without overwriting previous keys using the setdefault method. This method sets the value of a key only if it is not already present in the dict. The only catch is that it only handles one key at a time, so you would need to put it in a loop or do it sequentially:

d.setdefault('gaining', 34)
d.setdefault('Tinga', 42)
d.setdefault('small', 39)
d.setdefault('legs', 13)
d.setdefault('gaining', 20)
print (d['gaining'])

34

OR

i = [('gaining', 34), 
     ('Tinga', 42), 
     ('small', 39), 
     ('legs', 13), 
     ('gaining', 20)]
for k, v in i:
    d.setdefault(k, v) 
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
-3

This will get you keys in a list

keyList = [x for x,y in d.items()]
zenprogrammer
  • 623
  • 1
  • 7
  • 20