(Go: >> BACK << -|- >> HOME <<)

Append: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Altered pages. Added doi. Formatted dashes. | Use this bot. Report bugs. | Suggested by Spinixster | Category:Programming constructs | #UCB_Category 54/70
→‎Python: lang="pycon"
Line 101:
 
===Python===
In [[Python (programming language)|Python]], use the list method "extend" or the infix operators <code>+</code> and <code>+=</code> to append lists.
<syntaxhighlight lang="pythonpycon">
>>> l = [1, 2]
>>> l.extend([3, 4, 5])
>>> l
print l + [6, 7]
[1, 2, 3, 4, 5]
print>>> >>> l + [6, 7]
[1, 2, 3, 4, 5, 6, 7]
</syntaxhighlight>
After executing this code, l is a list containing [1, 2, 3, 4, 5], while the output generated is the list [1, 2, 3, 4, 5, 6, 7].
 
Do not confuse with the list method "append", which adds a '''single''' element to a list:
<syntaxhighlight lang="pythonpycon">
>>> l = [1, 2]
>>> l.append(3)
>>> l
[1, 2, 3]
 
</syntaxhighlight>
Here, the result is a list containing [1, 2, 3].
 
===Bash===