Better way to get dictionary values that are strings in a list or a string

Question:

#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?

Asked By: Anushka

||

Answers:

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)

Answered By: THUNDER 07

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']
Answered By: codester_09
','.join(mydict.values())

what about just join them all?

Answered By: wu hoyt

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']
Answered By: Vlad
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(',')
l = [i.replace(' ', '') for i in l]

print(l)
Answered By: dartigan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.