-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 
8
  • 2
    Dictionaries can't have duplicate keys. Commented Jan 20, 2017 at 6:26
  • @Andy Is there no way at all around it? Commented Jan 20, 2017 at 6:27
  • The keys are available as a list as .keys() Commented Jan 20, 2017 at 6:28
  • 2
    Duplicate key is not a problem. It is just the way dictionaries work. Commented Jan 20, 2017 at 6: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 and How to Ask. Commented Jan 20, 2017 at 6:33

4 Answers 4

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) 
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

How is [k for k in d.keys()] different from d.keys() or list(d.keys())?
@DYZ I guess it isn't but could make it easier to adapt it to other print formats?
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].

Comments

-3

This will get you keys in a list

keyList = [x for x,y in d.items()] 

3 Comments

... or just use .keys()
That's called d.keys().
yes that's obvious :) only giving another option to poster to get more understanding about dictionaries, lists and iteration.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.