2

In my code I have for loop to a dict... and I get desired output as shown below..

dict = {"apple":5,"banana":3, "mangos":2}
columns = ('apple', 'banana','mangos')
for column in columns:
    value = dict.get(column)
    print column,value

Output:

apple 5
banana 3
mangos 2

But if my dict changes to

dict = {"apple":5,"Oranges":3, "mangos":2}

the same for loop would give me following output

columns = ('apple', 'banana','mangos')
for column in columns:
    value = dict.get(column)
    print column,value

I get following, which is expected

apple 5
banana None
mangos 2

Now the question is, is there a way I can set the column loop

columns = ('apple', 'banana','mangos')

so that the second value 'banana' could be either 'banana' or 'orange' ?

7
  • 1
    what would you want the output to be in the second example? Commented Oct 28, 2016 at 14:37
  • 2
    why do you have to predefine the columns and not just iterate over the dict keys? Then it will be banana if the dict contains bananas or orange if it contains orange as keys. Commented Oct 28, 2016 at 14:38
  • @Ev.Kounis Perhaps because order matters, in which case an OrderedDict would be the way to go Commented Oct 28, 2016 at 14:38
  • In the second example i would want the output to be Oranges 3.... Commented Oct 28, 2016 at 14:39
  • 1
    An ad hoc solution to this particular question is trivial, like dict.get(column, dict.get('orange')), but I strongly suspect this is an XY problem. What are you trying to do? Commented Oct 28, 2016 at 14:40

2 Answers 2

3
dict = {"apple":5,"oranges":3, "mangos":2}
columns = ('apple', 'banana' , 'oranges','mangos')
for column in columns:
    value = dict.get(column)
    if value:
        print(column,value)

Check if the value is there or not and then print it or OrderedDict, There's too many good answers, you could check if in the dict has 'oranges' or 'banana' and then change your columns based on what you find.

dict = {"apple":5,"oranges":3, "mangos":2}
if 'banana' in dict:  
    columns = ('apple', 'banana','mangos') 
else:
    columns = ('apple', 'oranges','mangos') 
for column in columns:
    value = dict.get(column)
    print(column,value)

Edited to use if 'banana' in dict because of Ev. Kounis's comment! Thanks.

Sign up to request clarification or add additional context in comments.

2 Comments

I think it would be better for the check to be if 'banana' not in dict since it might be in there with a value that has a bool of False like 0 for example. Also the name dict is to be avoided since it shadows the built-in
Oh true! I forgot about that, still stupidly early where I live, thank you will update
0
d = {"apple":5,"banana":3, "orange":2}
c = ('apple', 'banana','mangos')

# print direct values
for k, v in d.items():
    print k, v

# print any values of the predefined columns + additionnal ones
ext = set(list(c) + d.keys())
for col in ext:
    print col, d.get(col)

Ouput 1 :

orange 2

apple 5

banana 3

Ouput 2:

orange 2

mangos None

apple 5

banana 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.