0

Why isn't it working properly?

a = [] b = input().split(" ") p = [] for i in b: a.append(int(i)) e = input().split(" ") for w in e: p.append(int(w)) (a+p).sort() print(a+p)

9th Aug 2021, 3:37 AM
Shahir
Shahir - avatar
5 Answers
+ 1
1. Because you sorted two lists together. But didn't save result in any variable. 2. The code will give error if to enter space more than once at once. 3.code will give error if to enter anything but digit.
9th Aug 2021, 4:28 AM
Shadoff
Shadoff - avatar
+ 1
Shahir, (a+p).sort() here a + p creates transient array with all elements of array a and array p. And call to sort() on this transient array is get sorted. It doesn't affect array a, p. So, sort() call works properly. Result of sort() call is not available. DHANANJAY PATEL
9th Aug 2021, 5:24 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
Task is to sort the integers of two arrays Example Input: [1,3,7] [0,2,4] Output: 0,1,2,3,4,7
9th Aug 2021, 4:53 AM
Shahir
Shahir - avatar
0
list.sort() sorts the list in place https://python-reference.readthedocs.io/en/latest/docs/list/sort.html you need to store your list inside the variable and then use sort() on it. https://code.sololearn.com/c2LK0e135a9M/?ref=app
9th Aug 2021, 7:30 AM
Ratnapal Shende
Ratnapal Shende - avatar
0
Shahir The new sorted list (a+p) is garbage collected as there's no reference to it saved in the code. Try using the sorted() function instead.
9th Aug 2021, 8:05 AM
Calvin Thomas
Calvin Thomas - avatar