Oktatás
* Programozás 1 + feladatsor + GitHub oldal
* Szkriptnyelvek + feladatsor + quick link
Teaching
* Programming 1 (BI) ◇ exercises ◇ quick link
teaching assets
Félévek
* 2024/25/2 * archívum
Linkek
* kalendárium - munkaszüneti napok '20 * tételsorok * jegyzetek * szakdolgozat / PhD * ösztöndíjak * certificates * C lang. * C++ * C# * Clojure * D lang. * Java * Nim * Scala
[ edit | logout ] [ sandbox | passwd ]
|
Append to a list, extend a list
(1) append, extend
|
def main():
li = [1, 2, 3]
numbers = [50, 60]
li.append(4)
print(li) # [1, 2, 3, 4]
nums1 = li.copy() # [1, 2, 3, 4] (copy)
nums1.append(numbers)
print(nums1) # [1, 2, 3, 4, [50, 60]]
nums2 = li.copy() # [1, 2, 3, 4] (copy)
nums2.extend(numbers)
print(nums2) # [1, 2, 3, 4, 50, 60]
|
import std.stdio;
void main()
{
int[] li = [1, 2, 3];
int[] numbers = [50, 60];
li ~= 4;
writeln(li); // [1, 2, 3, 4]
auto nums1 = li.dup; // [1, 2, 3, 4] (copy)
nums1 ~= numbers;
writeln(nums1); // [1, 2, 3, 4, 50, 60]
writeln(li); // [1, 2, 3, 4]
}
|
In D, you can append to a list with the ~= operator. If you append a single element, it'll be added. If you append another list, then each element of it will be appended. That is, it works similarly to Python's list.extend() method.
Create a list of lists
(2) list of lists
|
def main():
li1 = [1, 2, 3]
li2 = [50, 60]
result = []
result.append(li1)
result.append(li2)
print(result) # [[1, 2, 3], [50, 60]]
|
import std.stdio;
void main()
{
int[] li1 = [1, 2, 3];
int[] li2 = [50, 60];
int[][] result; // list of lists (2D)
result ~= li1;
result ~= li2;
writeln(result); // [[1, 2, 3], [50, 60]]
}
|
|
Blogjaim, hobbi projektjeim
* The Ubuntu Incident * Python Adventures * @GitHub * heroku * extra * haladó Python * YouTube listák
Debrecen | la France
[ edit ]
|