Combine and Sort Lists in Python
In Python, the + operator can be used to combine two lists. For example:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list1 + list2
print(merged_list)
Output:
[1, 2, 3, 4, 5, 6]
To sort a list, you can use the sort() method or the sorted() function. The sort() method will directly sort the original list, while the sorted() function will return a new sorted list. For example:
Using the sort() method:
my_list = [3, 1, 2]
my_list.sort()
print(my_list)
Output:
[1, 2, 3]
Use the sorted() function:
my_list = [3, 1, 2]
sorted_list = sorted(my_list)
print(sorted_list)
output:
[1, 2, 3]