Answer the question
In order to leave comments, you need to log in
How to make a list from a value in a python dictionary?
Hello. There is a dictionary:
{0: "Hi", 1: "Monty", 2: "Python", 3: "snake", 4: "Anaconda"}
How can I add a name to a 0 key by converting it to a list?
For example, I need to get this from the dictionary above:
{0: ["Hi", "Hello"], 1: "Monty", 2: "Python", 3: "snake", 4: "Anaconda"}
Answer the question
In order to leave comments, you need to log in
a = {0: "Hi", 1: "Monty", 2: "Python", 3: "snake", 4: "Anaconda"}
a[0] = [a[0], 'Hello']
print(a)
# {0: ['Hi', 'Hello'], 1: 'Monty', 2: 'Python', 3: 'snake', 4: 'Anaconda'}
def append(data, key, val):
if key not in data:
data[key] = []
elif type(data[key]) != list:
data[key] = [ data[key] ]
data[key].append(val)
a = {0: "Hi", 1: "Monty", 2: "Python", 3: "snake", 4: "Anaconda"}
x = [a[0]]
x.append("Hello")
a[0] = x
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question