Skip to content
Advertisement

Combine two lists and sort them

Lets say I have two lists. I want to append list2 into list1 and then sort and add a new element at a specific index. I keep getting an error message saying:

TypeError: ‘<‘ not supported between instances of ‘list’ and ‘int’

This is what I have tried:

list1 = [11, -21, 23, 45, 66, -93, -21]
list2 = [15, 67, -40, -21, 10]
list1.append(list2)
list1.insert(4, 50)
print(list1.sort())

Advertisement

Answer

Use sorted() if you want to print sorted list:

list1 = [11, -21, 23, 45, 66, -93, -21]
list2 = [15, 67, -40, -21, 10]
list1.insert(4, 50)
print(sorted(list1 + list2))

# [-93, -40, -21, -21, -21, 10, 11, 15, 23, 45, 50, 66, 67]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement