# how to get specified order of strings in list ?

**URL:** <https://forum.ansible.com/t/how-to-get-specified-order-of-strings-in-list/32182>\
**Category:** Ansible Project\
**Created:** [February 10, 2020, 3:34am UTC](https://forum.ansible.com/t/how-to-get-specified-order-of-strings-in-list/32182 "2020-02-10T03:34:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [February 10, 2020, 3:34am UTC](https://forum.ansible.com/t/how-to-get-specified-order-of-strings-in-list/32182/1 "2020-02-10T03:34:21Z")

</div>

I have a list like this

user:

name: chchang  
comment: “Chang Chic Hung”

and I want to add more attrs like

last\_name: Chang  
mid\_name: Chic

but how to get the specifed order of the values ?  
I try to use filter “first” , but it just return “C” , the first letter , not the first word.

any suggestions ?

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [February 10, 2020, 4:10am UTC](https://forum.ansible.com/t/how-to-get-specified-order-of-strings-in-list/32182/2 "2020-02-10T04:10:30Z")

</div>

Do you want to extract the strings from the comment, or do you just want to get the three attributes (last\_name, mid\_name etc) in some particular order?

You can get the parts of the name out of the comment by using the split() filter. This assumes no names contain spaces or whatever split delimiter you use - not a good assumption. Ask Charles de Gaulle.

There is no automatable way to reliably work out what order the parts of a name should be in. Some cultures are firstname, lastname, some are lastname firstname, some are one in some circumstances and the other way in other circumstances, some routinely have three parts, or only one part. Even in the English-speaking world there are zillions of exceptions like J. Edgar Hoover or Fred Smith Junior.

Regards, K

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 10, 2020, 4:43am UTC](https://forum.ansible.com/t/how-to-get-specified-order-of-strings-in-list/32182/3 "2020-02-10T04:43:59Z")

</div>

The tasks

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ user|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({'last\_name': comment\_split.0})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({'mid\_name': comment\_split.1}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment\_split: "{{ user.comment.split() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: user

give

&nbsp;&nbsp;&nbsp;&nbsp;"user": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"comment": "Chang Chic Hung",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"last\_name": "Chang",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mid\_name": "Chic",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "chchang"  
&nbsp;&nbsp;&nbsp;&nbsp;}

HTH,

&nbsp;&nbsp;-vlado
