Reputation: 309
Initially, we have name and numbers inside list, it is necessary to convert all in new list
name1 = [u'pPlane1']
list1 = [19, 20, 21, 22, 24]
The desired result:
list2 = ['pPlane1.e[19]', 'pPlane1.e[20]', 'pPlane1.e[21]', 'pPlane1.e[22]', 'pPlane1.e[24]']
I would be grateful for any help :)
Upvotes: 0
Views: 103
Reputation: 387657
You can use list comprehension to create the new list directly from the source. By using string formatting, you can create the target string of each element nicely.
list2 = ['{}.e[{}]'.format(name1[0], num) for num in list1]
If name1
contains multiple names, you could create a cartesian product like this:
list2 = ['{}.e[{}]'.format(name, num) for num in list1 for name in name1]
For example:
>>> name1 = ['pPlane1', 'pPlane2']
>>> list1 = [19, 20, 21]
>>> ['{}.e[{}]'.format(name, num) for num in list1 for name in name1]
['pPlane1.e[19]', 'pPlane2.e[19]', 'pPlane1.e[20]', 'pPlane2.e[20]', 'pPlane1.e[21]', 'pPlane2.e[21]']
Upvotes: 4