How to append variables in nested list in python
Question:
if __name__ == '__main__':
for _ in range(int(input())):
name = input()
score = float(input())
a=[]
a.append([name][score])
print(a)
This is error occurred when I insert values
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/Nested Lists.py", line 6, in <module>
a.append([name][score])
TypeError: list indices must be integers or slices, not float
Answers:
Use a dictionary instead of a list (a list will work, but for what you are doing a hashmap is better suited):
if __name__ == '__main__':
scores = dict()
for _ in range(int(input())):
name = input()
score = float(input())
scores[name] = score
print(scores)
The syntax to make a list containing name
and score
is [name, score]
. [name][score]
means to create a list containing just [name]
, and then use score
as an index into that list; this doesn’t work because score
is a float
, and list indexes have to be int
.
You also need to initialize the outer list only once. Putting a=[]
inside the loop overwrites the items that you appended on previous iterations.
a=[]
for _ in range(int(input())):
name = input()
score = float(input())
a.append([name, score])
print(a)
As others have told, a dictionary is probably the best solution for this case.
However, if you want to add an element with multiple values to a list, you have to create a sublist a.append([name, score])
or a tuple a.append((name, score))
.
Keep in mind that tuples can’t be modified, so if you want, for instance, to update the score of a user, you must remove the corresponding tuple from the list and add a new one.
In case you just want to add new values to a flat list, simply go for a = a + [name, score]
. This will add both name
and score
to the end of the list as entirely independent elements.
if __name__ == '__main__':
deva=[]
for _ in range(int(input())):
name = input()
score = float(input())
l=[]
l.append(name)
l.append(score)
deva.append(l)
print(deva)
if __name__ == '__main__':
for _ in range(int(input())):
name = input()
score = float(input())
a=[]
a.append([name][score])
print(a)
This is error occurred when I insert values
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/Nested Lists.py", line 6, in <module>
a.append([name][score])
TypeError: list indices must be integers or slices, not float
Use a dictionary instead of a list (a list will work, but for what you are doing a hashmap is better suited):
if __name__ == '__main__':
scores = dict()
for _ in range(int(input())):
name = input()
score = float(input())
scores[name] = score
print(scores)
The syntax to make a list containing name
and score
is [name, score]
. [name][score]
means to create a list containing just [name]
, and then use score
as an index into that list; this doesn’t work because score
is a float
, and list indexes have to be int
.
You also need to initialize the outer list only once. Putting a=[]
inside the loop overwrites the items that you appended on previous iterations.
a=[]
for _ in range(int(input())):
name = input()
score = float(input())
a.append([name, score])
print(a)
As others have told, a dictionary is probably the best solution for this case.
However, if you want to add an element with multiple values to a list, you have to create a sublist a.append([name, score])
or a tuple a.append((name, score))
.
Keep in mind that tuples can’t be modified, so if you want, for instance, to update the score of a user, you must remove the corresponding tuple from the list and add a new one.
In case you just want to add new values to a flat list, simply go for a = a + [name, score]
. This will add both name
and score
to the end of the list as entirely independent elements.
if __name__ == '__main__':
deva=[]
for _ in range(int(input())):
name = input()
score = float(input())
l=[]
l.append(name)
l.append(score)
deva.append(l)
print(deva)