Чтобы добавить значения из одного списка в другой, вы можете использовать различные методы в разных языках программирования. Вот несколько примеров:
Python:
# Method 1: Using the extend() method
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]
# Method 2: Using the + operator
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = list1 + list2
print(list3) # Output: [1, 2, 3, 4, 5, 6]
# Method 3: Using the append() method in a loop
list1 = [1, 2, 3]
list2 = [4, 5, 6]
for item in list2:
list1.append(item)
print(list1) # Output: [1, 2, 3, 4, 5, 6]
JavaScript:
// Method 1: Using the concat() method
let list1 = [1, 2, 3];
let list2 = [4, 5, 6];
let list3 = list1.concat(list2);
console.log(list3); // Output: [1, 2, 3, 4, 5, 6]
// Method 2: Using the push() method in a loop
list1 = [1, 2, 3];
list2 = [4, 5, 6];
for (let i = 0; i < list2.length; i++) {
list1.push(list2[i]);
}
console.log(list1); // Output: [1, 2, 3, 4, 5, 6]
Java:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Method 1: Using the addAll() method
List<Integer> list1 = new ArrayList<>(List.of(1, 2, 3));
List<Integer> list2 = new ArrayList<>(List.of(4, 5, 6));
list1.addAll(list2);
System.out.println(list1); // Output: [1, 2, 3, 4, 5, 6]
// Method 2: Using a loop and the add() method
list1 = new ArrayList<>(List.of(1, 2, 3));
list2 = new ArrayList<>(List.of(4, 5, 6));
for (int item : list2) {
list1.add(item);
}
System.out.println(list1); // Output: [1, 2, 3, 4, 5, 6]
}
}
Эти методы позволяют добавлять значения из одного списка в другой в Python, JavaScript и Java.