Is there any way to make dictionary key,value pairs to tuple?
Question:
I need to convert this dictionary:
{'A': 0, 'B': 1290, 'C': 515, 'D': 600}
Into this test case:
(A : 0) - (B : 1290) - (C : 515) - (D : 600)
This is how I derive my dictionary
def stock_list(list_of_art, list_of_cat):
new_dictionary = {}
numbers = []
for character in list_of_cat:
if character not in new_dictionary:
new_dictionary[character] = 0
for word in list_of_art:
first_element = word[0][0]
for number in word:
if number.isdigit():
numbers.append(number)
make_str = "".join(numbers)
numbers = []
if first_element in new_dictionary:
new_dictionary[first_element] += int(make_str)
return new_dictionary
This is a sample call
stock_list(["BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"],["A","B","C","D"])
Answers:
Just iterate over the keys and values of your dictionary, and format them into a string.
d = {'A': 0, 'B': 1290, 'C': 515, 'D': 600}
print(" - ".join(f'({k} : {v})' for k,v in d.items()))
#(A : 0) - (B : 1290) - (C : 515) - (D : 600)
I need to convert this dictionary:
{'A': 0, 'B': 1290, 'C': 515, 'D': 600}
Into this test case:
(A : 0) - (B : 1290) - (C : 515) - (D : 600)
This is how I derive my dictionary
def stock_list(list_of_art, list_of_cat):
new_dictionary = {}
numbers = []
for character in list_of_cat:
if character not in new_dictionary:
new_dictionary[character] = 0
for word in list_of_art:
first_element = word[0][0]
for number in word:
if number.isdigit():
numbers.append(number)
make_str = "".join(numbers)
numbers = []
if first_element in new_dictionary:
new_dictionary[first_element] += int(make_str)
return new_dictionary
This is a sample call
stock_list(["BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"],["A","B","C","D"])
Just iterate over the keys and values of your dictionary, and format them into a string.
d = {'A': 0, 'B': 1290, 'C': 515, 'D': 600}
print(" - ".join(f'({k} : {v})' for k,v in d.items()))
#(A : 0) - (B : 1290) - (C : 515) - (D : 600)