-1

Can you please guide, how I iterate dictionary in python.

How I get it one by one like key: value key: value My Code:

def dist(dict):
    # z = None
    for i in dict:

        print(dict[i])
        z = i+": " + dict[i]
        # print(z)
        # return z

if __name__ == '__main__':
    k = dist({'B_weeks': '40.0 week, 6.0 day, 20.0 hour, 30.0 minute', 'S_weeks': '2.0 week, 3.0 day, 19.0 hour, 59.0 minute'})
    print(k)

Recommended output:

   'B_weeks': '40.0 week, 6.0 day, 20.0 hour, 30.0 minute'
   'S_weeks': '2.0 week, 3.0 day, 19.0 hour, 59.0 minute'

Ho I return this recommended output in one variable from the given function. because i want to use it in another function.

  • 1
    `for key, value in dict.items()` – Alexander Lekontsev Apr 01 '20 at 09:00
  • Does this answer your question? [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – dspencer Apr 01 '20 at 09:02
  • instead of ```for i in dict:``` – user7917919 Apr 01 '20 at 09:03
  • 1
    What do you mean by "return this recommended output in one variable"? Do you want to return a string of the dict content, or an iterator over key, value pairs? Are you aware that you can just call ``str`` on a dict? – MisterMiyagi Apr 01 '20 at 09:07
  • @MisterMiyagi, I am passing dictionary like ```{'B_weeks': '40.0 week, 6.0 day, 20.0 hour, 30.0 minute', 'S_weeks': '2.0 week, 3.0 day, 19.0 hour, 59.0 minute'}``` And want output of function like Recommended output , So how can i get it – user7917919 Apr 01 '20 at 09:23
  • If your dictionary is in `data` you can use `print('\n'.join(f'{key}: {value}' for key, value in data.items()))`. – Matthias Apr 01 '20 at 10:45

4 Answers4

0

Take a variable, say output and now iterate over the key value pairs and concatenate to the string. Then return the string.

output = ""
for k, v in dict.items():
    output += "{}: {} ".format(k,v)

return output
masnun
  • 11,635
  • 4
  • 39
  • 50
  • Giving o/p like : ``` B_weeks: 40.0 week, 6.0 day, 20.0 hour, 30.0 minuteS_weeks: 2.0 week, 3.0 day, 19.0 hour, 59.0 minute``` but we need o/p like mention in recommended o/p. NOT IN SINGLE LINE – user7917919 Apr 01 '20 at 09:10
  • please see recommended output from my question. need like that. output should not in single line – user7917919 Apr 01 '20 at 09:16
  • This is probably missing a ``"\n"`` instead of a ``" "``. (Guessing from the "recommended output" in the question.) – MisterMiyagi Apr 01 '20 at 09:25
  • @MisterMiyagi, where exactly need to use ```"\n"``` – user7917919 Apr 01 '20 at 09:27
0

Use .items()

def dist(k):
    items = k.items()
    for key, value in items:
        print('"{}": "{}"'.format(key, value))
    return items


if __name__ == '__main__':
    k = dist({'B_weeks': '40.0 week, 6.0 day, 20.0 hour, 30.0 minute', 'S_weeks': '2.0 week, 3.0 day, 19.0 hour, 59.0 minute'})
    print(k)
Victor
  • 2,864
  • 1
  • 12
  • 20
  • yopur code returing ```dict_items([('B_weeks', '40.0 week, 6.0 day, 20.0 hour, 30.0 minute'), ('S_weeks', '2.0 week, 3.0 day, 19.0 hour, 59.0 minute')])``` need output like mentioned in Recommended output: – user7917919 Apr 01 '20 at 09:18
0
def dict_comprehension(dictionary):
    out=''
    for key,val in dictionary:
        out+="{}:{}".format(key,val)
    return out
Aroo
  • 55
  • 1
  • 10
  • its gives output like ```B_weeks:40.0 week, 6.0 day, 20.0 hour, 30.0 minuteS_weeks:2.0 week, 3.0 day, 19.0 hour, 59.0 minute ``` – user7917919 Apr 01 '20 at 09:44
  • pls refer Recommended output: – user7917919 Apr 01 '20 at 09:44
  • To get that output, return the value as a list. Let me ask this,Why do you need to call a function for such a simple situation. because you are wasting a lot of memory in calling and creating the stack. – Aroo Apr 01 '20 at 09:49
0

You can try the code below.

Code example:

def dist(dictionary):
    out=''
    for key,val in dictionary.items():
        out+="'{}':'{}'\n".format(key,val)
    return out


if __name__ == '__main__':
    k = dist({'B_weeks': '40.0 week, 6.0 day, 20.0 hour, 30.0 minute', 'S_weeks': '2.0 week, 3.0 day, 19.0 hour, 59.0 minute'})
    print(k)

I have a question, why need the single quote in your recommended output?

Lurima
  • 52
  • 3