Convert List into multiple dictionaries

Hello,

I’m currently facing a problem that i need some help with.

I have a list that contains documentnames and documentkinds, there are now three of each but there could also be less or more depending on the situation. The list is supplied in order of documentname and documentkind. For example:

@{documents} doc_.doc main doc_2.doc appendix doc_3.doc background

In order to use a keyword I need to supply a list of dictionaries that looks like this:

@{list} {document1} {document2} ${document3}

The dictionaries look like:

${document1} documentname=doc_.doc documentkind=main

${document2} documentname=doc_2.doc documentkind=appendix

${document3} documentname=doc_3.doc documentkind=background

I’m trying to create a loop that handles this but I can’t quite get it there:

FOR ${document} IN @{documents}

{document_name} Get From List {documents} 0
{document_kind} Get From List {documents} 1
&{document1} Create Dictionary documentname={document_name} documentkind={document_kind}

END

I need some helping with increasing the position indicator with 2 everytime the loop finishes, so 0, 2, 4, 6, 8 for the document_names and 1, 3, 5, 7, 9 for the document kinds.

And a way to increment the dictionary name by 1, so &{document1}, &{document2}, &{document3}.

Hi David,

would this do what you want?

${i} =	Set Variable		0
FOR ${document} IN @{documents}

{document_name} Get From List {documents} ${i}
${i} =	Evaluate		${i} + 1
{document_kind} Get From List {documents} ${i}
${i} =	Evaluate		${i} + 1
&{document1} Create Dictionary documentname={document_name} documentkind={document_kind}

END

Dave.

Hi Dave,

Yes, that loop worked perfect! Thank you :slight_smile:
Do you also know how to increment the name of my dictionary? Now it’s constantly overwriting &{document1}

Yeah, I wrote that comment pretty quickly without thinking too hard, try this:

*** settings ***
Library		Collections

*** test cases ***
eval test

	@{documents}	Create List		doc_.doc	main	doc_2.doc	appendix	doc_3.doc	background

	${i} =	Set Variable		0
	${d} =	Set Variable		0
	${count}	Get length		${documents}
	${count}=	Evaluate		${count} / 2
	FOR 	${document} 	IN RANGE	${count}

		${document_name} 	Get From List	${documents} 	${i}
		${i} =	Evaluate		${i} + 1
		${document_kind} 	Get From List	${documents} 	${i}
		${i} =	Evaluate		${i} + 1

		${d} =	Evaluate		${d} + 1
		Set Test Variable	&{document${d}}	documentname=${document_name}	documentkind=${document_kind}

	END

RF is really awesome in the way it handles variables

That is awesome! Yes, this is exactly what needed to be done :slight_smile: thank you!

1 Like