Home > other >  Better way to get dictionary values in a list or a sting. Each value is a string consisting of multi
Better way to get dictionary values in a list or a sting. Each value is a string consisting of multi

Time:07-01

#the dictionary is 
mydict = {'fruits': 'banana, apple,grapefruit', 
'vegetables': 'tomato, potato,brocolli', 
'dry fruits': 'cashew, almond' }

#expected output can be either list or string

banana, apple,grapefruit,tomato,potato,brocolli,cashew,almond

what I have until now is to put the values in a list and iterate through each element of the list and split each string

mydict = {'fruits': 'banana, apple,grapefruit', 
'vegetables': 'tomato, potato,brocolli', 
'dry fruits': 'cashew, almond' }
newlist = ''
mylist = list(mydict.values())
for ele in mylist:
    x = ele.replace(' ', '').strip(',')
    newlist = newlist   x  ','
print(newlist)

Is there a better way to do it? Please help me out! Thank you so much.

CodePudding user response:

To create a list from the comma-separated values you could do this:

mydict = {'fruits': 'banana, apple,grapefruit',
          'vegetables': 'tomato, potato,brocolli',
          'dry fruits': 'cashew, almond'}

list_ = list(map(str.strip, ','.join(mydict.values()).split(',')))

print(list_)

Output:

['banana', 'apple', 'grapefruit', 'tomato', 'potato', 'brocolli', 'cashew', 'almond']

CodePudding user response:

','.join(mydict.values())

what about just join them all?

CodePudding user response:

mydict = {'fruits': 'banana, apple,grapefruit', 
'vegetables': 'tomato, potato,brocolli', 
'dry fruits': 'cashew, almond' }

l = [v for (k,v) in mydict.items()]
l = ','.join(l)
l = l.split(',')
print(l)

CodePudding user response:

For string, you can directly use the join method ( Which is commonly used to convert a list to a string).

And, for a list, you can loop through all the values and use the split method to make them into a list. then use the map and strip methods to remove unnecessary spaces from the string.

mydict = {'fruits': 'banana, apple,grapefruit', 
'vegetables': 'tomato, potato,brocolli', 
'dry fruits': 'cashew, almond' }
# for string
mystring = ','.join(mydict.values())
print(f'String output = {mystring}')

# for list
mylist = []
for a in mydict.values():
    l = map(lambda e:e.strip(),a.split(','))
    mylist.extend(l)
print(f'list output = {mylist}')

OUTPUT

String output = banana, apple,grapefruit,tomato, potato,brocolli,cashew, almond
list output = ['banana', 'apple', 'grapefruit', 'tomato', 'potato', 'brocolli', 'cashew', 'almond']

CodePudding user response:

split values at , and add to new list strip() is to remove leading and trailing spaces in string. item returns a 2d array with each row containing key and value.

mydict = {'fruits': 'banana, apple,grapefruit',
'vegetables': 'tomato, potato,brocolli',
'dry fruits': 'cashew, almond' }
newlist = []
mylist = list(mydict.values())
for key,val in mydict.items():
    for value in val.split(','):
        newlist.append(value.strip())
print(newlist)

  • Related