You have a very confused implementation of a very simple operation.
number = 1 for i in list_: number += 1
With this cycle you are trying to count the number of items in the list. Why not do it like this:
number = len(list_)
Then we have
a = input() number = len(list_) task = {"name":a, "num":number} list_.append(task)
Further, you can not declare a task variable:
a = input() number = len(list_) list_.append({"name":a, "num":number})
Let's make some more modifications
list_.append({"name":input(), "num":len(list_)})
But perhaps input() makes sense to put in a separate line in order to process the EOF .
These modifications are also important for several reasons:
- You save yourself from having to invent variable names (sometimes it’s important to create a new variable, especially when the code on one line becomes too much), which can be difficult to do in an hour. To call a variable anyhow is not good, since the code consisting of the variables
a , b , c , xix , etc. difficult to maintain. - Secondly, time and memory are spent on creating a variable (in this case it is a trifle), but on a large scale it can play a role (As for python, I’m not sure how variables are created, but I suspect that these resources are spent on it)
In addition, I suggest you make a replacement with either enum or named tuples. Dictionaries are not relevant here, since you have a fixed number of keys in advance. Why not replace key lines with ints? They weigh an order of magnitude less.
This note is important because when you scale your piece of code to a large amount of data, you will most likely encounter a problem of insufficient RAM. After that, you will have to redo your algorithm, which by that moment, probably, will be associated with a large number of other systems and algorithms.
To create an enum , import the class:
from enum import Enum class Keys(Enum): Name = 0 Number = 1 list_.append({Keys.Name:input(), Keys.Number:len(list_)})
In python, enum can do interesting things .
A similar example can be given for namedtuple :
from collections import namedtuple Keys = namedtuple('Keys', ['name', 'number']) list_.append(Keys(input(), len(list_)))
Ps And, probably, the number must, nevertheless, be replaced by count .
list =codeprint(list("123"))will work, call neutral better) 2 you can shorten your cycle withnumber:number = len(items)- gil9red