-1

I have this list:

x = ['nm0000131', 'nm0000432', 'nm0000163']

And I would like to convert it to:

'nm0000131', 
'nm0000432', 
'nm0000163'

e.g: I would like convert a list of strings (x) to 3 independent strings.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94

1 Answers1

0

If you want three separate string you can use for loop.

Try the following code:

x = ['nm0000131', 'nm0000432', 'nm0000163']
for value in x:
    print(value) 

Output will be like:

nm0000131                                                                                                                      
nm0000432                                                                                                                      
nm0000163

The following code will display an output like "nm0000131" ,"nm0000432" ,"nm0000163":

x = ['nm0000131', 'nm0000432', 'nm0000163']
str1 = '" ,"'.join(x)
x = '"'+str1+'"'
print(x)

As you mentioned in the comment I would like to include some more points to my answer. If you want to get the key-value pairs then try the following code.

y = {'131': 'a', '432': 'b', '163': 'c'}
w = []
for key, value in y.items():
    w.append(value)

print(w)

Output:

['c', 'a', 'b'] 
piet.t
  • 11,718
  • 21
  • 43
  • 52
MjM
  • 571
  • 4
  • 15
  • Good catch but this code convert a list for 1 only string: '"nm0000131" ,"nm0000432" ,"nm0000163"' I would like convert for 3 strings like: 'nm0000131', 'nm0000432', 'nm0000163' – Alexandre Cominotte Mar 28 '19 at 14:49
  • I have changed my code so look at it. I think this is what you are looking for. If my answer is correct one then click on the tick to accept my answer because which will help some one later. – MjM Mar 28 '19 at 15:52
  • Got it, Now I'm working with a list and a dictionary, I would like from the value of the list to look up the corresponding in the dictionary and create a list with the corresponding, like: x = ['131', '432', '163'] y = {'131':a, '432': b, '163': c} Output: w = ['a', 'b', 'c'] Thanks! – Alexandre Cominotte Mar 29 '19 at 01:05
  • Use a `for loop` and iterate through the `key-value pairs` then assign the value into another `array`. – MjM Mar 29 '19 at 05:16
  • I have updated my answer :) – MjM Mar 29 '19 at 09:25
  • You are welcome. Hi friend if my answer helped you then accept it. – MjM Mar 30 '19 at 11:37