Home > Mobile >  How add value in list inside list?
How add value in list inside list?

Time:05-01

python code if I have List [ [m,1],[n,5],[t,4] ]

I want if I input m and add 3, return me [m,4]

List [ [m,4],[n,5],[t,4] ]

but if I input f and add 3, return me [f,3]

List [ [m,1],[n,5],[t,4] ,[f,3] ]

This is what I do but it is wrong


main_list=[ ["m",1],["n",5],["t",4]  ]

def items_in_list(input,add):
    if any(input in sublist for sublist in main_list):
        print("we have it")
        sublist[1]=  add


    else:
        ele = [input,add]
        main.append(ele)


items=input("enter you items: ")
add=int(input("enter you add: "))
items_in_list(items,add)

print(main_list)

CodePudding user response:

You can nest a Python list within a Python list like so:

>>> plist = [["a", 1], ["b", 1]]
>>> plist[0][1]

The second line will output 1. Then addition would work as it usually does in Python:

>>> plist[0][1]  = 2
>>> plist[0][1] 
3

Nonetheless, D. Manasreh and Deepak already made a good point that what you are trying to achieve is better implemented with a dictionary. Yet, the answer to your question, how do you add a list within a list is what I described above, the only thing you did not do correctly is just making proper use of strings in Python, meaning you are missing "" around the characters in your list, quote:

List [ [m,1],[n,5],[t,4] ,[f,3] ]

This is what I do but it is wrong

Yes it is, since this line would output the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'm' is not defined

For the list to be a Python list try:

[["m", 1], ["n", 5], ["t", 4], ["f", 3]]

CodePudding user response:

I think it you should use a dictionary not a list in this case. You can do something like this:

main_dict={
    'm': 1,
    'n': 5,
    't': 4,
}

def items_in_list(input_name,to_add, main_dict):
    try:
        main_dict[input_name]  = to_add
    except:
        main_dict[input_name] = to_add

Example 1:

items_in_list('m',5, main_dict)
print(main_dict)

Output:

{'m': 6, 'n': 5, 't': 4}

Example 2:

items_in_list('k',5, main_dict)
print(main_dict)

Output:

{'m': 1, 'n': 5, 't': 4, 'k': 5}

CodePudding user response:

You are handling m, n, t like literals but actually they are undefined variables. A correct way of using literals in Python is enlosing literal by quotes.

li = [['m', 1], ['n', 5], ['t', 4]]

def add_to_group(name_group, li, increment):
    for list_element in li:
        # print(list_element)
        if list_element[0] == name_group:
            list_element[1]  = increment
    return li


li = add_to_group('m', li, increment=5)
print(li)

CodePudding user response:

This could help you.

kitchen_list=[ ['m',1],['n',5],['t',4]  ]

def items_in_list(item,add):
    if any(items in a for a in kitchen_list):
        lst = [[a[0],a[1] add] if item in a else a for a in kitchen_list]

    else:
        lst = kitchen_list.copy()
        lst.append([item,add])        
    
    return lst

items=input("enter you items: ")
add=int(input("enter you add: "))
kitchen_list = items_in_list(items,add)
print(kitchen_list)

output

IN: 'm', 1

OUT: [['m', 2], ['n', 5], ['t', 4]]


# -----------------------------#

IN: 'y', 12

OUT: [['m', 1], ['n', 5], ['t', 4], ['y', 12]]

  • Related