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:
JavaScript
x
6
1
list1 = [11, -21, 23, 45, 66, -93, -21]
2
list2 = [15, 67, -40, -21, 10]
3
list1.append(list2)
4
list1.insert(4, 50)
5
print(list1.sort())
6
Advertisement
Answer
Use sorted()
if you want to print sorted list:
JavaScript
1
7
1
list1 = [11, -21, 23, 45, 66, -93, -21]
2
list2 = [15, 67, -40, -21, 10]
3
list1.insert(4, 50)
4
print(sorted(list1 + list2))
5
6
# [-93, -40, -21, -21, -21, 10, 11, 15, 23, 45, 50, 66, 67]
7