clink
clink

Reputation: 213

Adding an element from a list to another list

I have a list containing ID's of students:

ID = [1,2,3]

and i have a table containing student names and their hobby:

student = [['Jack','Fishing'],['Alice','Reading'],['Mun','Football']]

I want to concatenate the ID to the first position of each sublist within the student list where i obtain:

[[1,'Jack','Fishing'],[2,'Alice','Reading'],[3,'Mun','Football']]

I tried:

for i in range(len(student)):
    student = ID[i] + student[i]

but I'm getting an error saying unsupported operand type.

Upvotes: 0

Views: 61

Answers (5)

Aaditya Ura
Aaditya Ura

Reputation: 12689

You don't need loop or anything just try:

ID = [1,2,3]
student = [['Jack','Fishing'],['Alice','Reading'],['Mun','Football']]

print(list(zip(ID,student)))

output:

[(1, ['Jack', 'Fishing']), (2, ['Alice', 'Reading']), (3, ['Mun', 'Football'])]

If you don't want nested list then:

print(list(map(lambda x:[x[0],*x[1]],zip(ID,student))))

output:

[[1, 'Jack', 'Fishing'], [2, 'Alice', 'Reading'], [3, 'Mun', 'Football']]

Upvotes: 0

weiwoxinyou
weiwoxinyou

Reputation: 11

just a little change:

ID = [1,2,3]
student = [['Jack','Fishing'],['Alice','Reading'],['Mun','Football']]
for i in range(len(student)):
    student[i].insert(0,ID[i])
print(student)

output:

[[1, 'Jack', 'Fishing'], [2, 'Alice', 'Reading'], [3, 'Mun', 'Football']]

Upvotes: 0

J. Sethi
J. Sethi

Reputation: 33

@clink when you write ID[i], it picks an element from the list ID. All the elements in the list ID are of type int. Also, all the elements in the students list are of type list. Hence when you use + operator between an int and list types you get the error

TypeError: unsupported operand type(s) for +: 'int' and 'list'

What you need to do is to put the int into a new list to get the results you are seeking. Below is the modified code:

ID = [1,2,3]
student = [['Jack','Fishing'],['Alice','Reading'],['Mun','Football']]
for i in range(len(student)):
    student[i] = [ID[i]] + student[i]

Output

[[1, 'Jack', 'Fishing'], [2, 'Alice', 'Reading'], [3, 'Mun', 'Football']]

Pay attention to a single change: ID[i] was changed to [ID[i]].

Upvotes: 1

Rakesh
Rakesh

Reputation: 82795

You can use zip with list comprehension

Ex:

ID = [1,2,3]
l = [['Jack','Fishing'],['Alice','Reading'],['Mun','Football']]

newList = [[i[0]]+i[1] for i in zip(ID, l)]
print(newList)

Output:

[[1, 'Jack', 'Fishing'], [2, 'Alice', 'Reading'], [3, 'Mun', 'Football']]

Upvotes: 3

sanit
sanit

Reputation: 1764

@Clink- The element you get from range(len(student)) is int type and trying merge with list. You should merge iterable types. That's why you are getting this error.

TypeError: unsupported operand type(s) for +: 'int' and 'list'

You can try solutions given by Rakesh or can follow below approach.

[std.insert(0, ID[idx]) for idx, std in enumerate(student)]

Output [[1, 'Jack', 'Fishing'], [2, 'Alice', 'Reading'], [3, 'Mun', 'Football']]

Upvotes: 0

Related Questions