Home > Back-end >  How to print first 5 keys and values from dictionary in Python for below code?
How to print first 5 keys and values from dictionary in Python for below code?

Time:01-03

I want to print first 5 keys and values from my dictionary but I can't do that. I'm trying with while loop in different position but it's not working for me. Please help me how can I do that.

Code:

def table_dict(dictionary):
    if isinstance(dictionary, str):
        return '<td>' dictionary '</td>'
    s = ['<!-- wp:table --><figure ><table><tbody>']
    for key, value in dictionary.items():
        s.append('<tr><td>%s</td>' % key)
        # s.append('<tr><td>' key '</td>')
        s.append('<td>%s</td>' % value)
        s.append('</tr>')
    s.append('</tbody></table></figure><!-- /wp:table -->')
    return ''.join(s)

dictionary = {"name" : "John", "age" : 35, "height" : 65, "country:": "US", "weight": "50 KG", "nationality": "N/A"}
print(table_dict(dictionary))

CodePudding user response:

You have clearly the problem that order is not guaranteed in dict - you could solve that using an ordereddict.

Under that scenario, to print the first n lines (maybe it is better to say entries), you could modify your code as follows:

def table_dict(dictionary, max_entries = 5):
    if isinstance(dictionary, str):
        return '<td>' dictionary '</td>'
    s = ['<!-- wp:table --><figure ><table><tbody>']
    for n, [key, value] in enumerate(dictionary.items()):
        if n == max_entries:
            break
        s.append('<tr><td>%s</td>' % key)
        # s.append('<tr><td>' key '</td>')
        s.append('<td>%s</td>' % value)
        s.append('</tr>')
    s.append('</tbody></table></figure><!-- /wp:table -->')
    return ''.join(s)

Using this logic, if a dictionary has more than max_entries element, the output will be truncated to the first max_entries.

CodePudding user response:

A dictionary does not contain any rows. It contains of keys and values.

If you want to print out the keys separately which you might refer as rows you have to do:

dictionary.pop("nationality")
for key, value in dictionary.items()
    print(key, ':', value)

This is how I understand your problem, if not please consider redescribing it.

CodePudding user response:

@Malo I am trying to get the first five keys and values. I am tryinig with this code. But still it showing 6 result.

def table_dict(dictionary):
    if isinstance(dictionary, str):
        return '<td>' dictionary '</td>'
    s = ['<!-- wp:table --><figure ><table><tbody>']
    i = 1
    while i < 5:
        for key, value in dictionary.items():
            s.append('<tr><td>%s</td>' % key)
            # s.append('<tr><td>' key '</td>')
            s.append('<td>%s</td>' % value)
            s.append('</tr>')
        s.append('</tbody></table></figure><!-- /wp:table -->')
        print(len(s))
        return ''.join(s)
    i = i   1

print(table_dict(dicts))

CodePudding user response:

Thanks all already I got my solution by nikeros mentioned method.

  • Related