0

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.

1
  • student = [ID[i]] + student[i] should work. Commented Mar 18, 2018 at 6:00

5 Answers 5

3

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']]
Sign up to request clarification or add additional context in comments.

2 Comments

could you separate the print statement? I want to create a new list for the output. So printing the new list should be what i need
@clink; Sure. Updated snippet.
1

@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]].

Comments

0

@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']]

Comments

0

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']]

Comments

0

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']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.