0
item_list = [x for x in notion_position_block.collection.get_rows()]
item_dict = {x.title: len(x.get_property('potential_hires')) for x in item_list}

Which gets me what I want: {'Full Stack Engineer': 3, 'Social Media Manager': 2, 'Backend Software Engineer': 1}

The dictionaries key/value + num of elements will never be the same

How can I get this into a string if keys + num of elements are consistently changing?

What I am ultimately trying to get is:

Full Stack Engineer 3 
Social Media Manager 2
Backend Software Engineer 1

Edit:

Is it possible to get the dictionary into a string with some version of

''.format()

5 Answers 5

1

If what you really want is a string, not the dictionary, I'd skip right to:

for x in notion_position_block.collection.get_rows():
    print(x.title, len(x.get_property('potential_hires')))

If you do still want that dictionary, you can skip the list. You're creating a list, then looping over it to make the dictionary. It seems easier to just go right for the dictionary right away.

item_dict = {x.title: len(x.get_property('potential_hires')) for x in notion_position_block.collection.get_rows()}

for title, total in item_dict.items():
    print(title, total)

Edit for "Is it possible to get the dictionary into a string with some version of ''.format()": I'd use str.join for that:

', '.join(f'{title}: {total}' for title, total in item_dict.items())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Sorry I did not put that in the initial post
0

Try this:

for x in item_dict.keys():
  print(x,item_dict[x])

Comments

0

I'm not sure If I understand you right, but this is how you can change this dictionary representation to the string form and put in a list.

Then if you want to have exactly the same outcome as you provided, you just have to make a basic for loop to print out the elements from the list

item_dict = {'Full Stack Engineer': 3, 'Social Media Manager': 2, 'Backend Software Engineer': 1}

string_rep = [(keys, values) for keys, values in item_dict.items()]
print(string_rep)

Comments

0

You can use the items() method to iterate over the key/value pairs of your dictionary:

>>> for key, value in item_dict.items():
...     print(key, value)
... 
Full Stack Engineer 3
Social Media Manager 2
Backend Software Engineer 1

Comments

0

For the: ‘ Full Stack Engineer 3 ’ it means 3 - dictionary number or how many this words involve in item_list? If first:

for i in item_dict.keys(): print(i,item_dict[i])

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.