Z
Z
Zagir Majidov2022-01-14 22:03:41
Python
Zagir Majidov, 2022-01-14 22:03:41

How to change a value in an array in Python?

There is a dictionary and an array:
list = [{"1": [1, 2]}]

And how to add a value to the list dictionary?
Tried doing .update and += and = : list["1"].update(3) and list["1"] += [3] and list["1"] = [3], but it worked:
[{ "1": [1, 2, [], 3]}]
Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2022-01-14
@Zagir-vip

change value in array

add value to list dictionary

You need to decide whether you want to change the value in the list, or add a key to the dictionary
list[0]['1'].append(3)

V
Vladimir Kuts, 2022-01-14
@fox_12

It's not entirely clear what you need to end up with:

lst = [{"1": [1, 2]}]   # называть переменную list - плохая практика
lst[0]['1'].append(3)
print(lst)
# [{'1': [1, 2, 3]}]

lst[0]['2'] = [4, 5]
print(lst)
# [{'1': [1, 2, 3], '2': [4, 5]}]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question