DEV Community

Mr #1738
Mr #1738

Posted on

List Concatenation and List Replication in ptyhon

The + operator can combine two lists to create a new list value in the sameway it combines two strings into a new string value. The * operator can also be used with a list and an integer value to replicate the list. Enter the fol-
lowing into the interactive shell and try out to see the output:

>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
>>> spam = [1, 2, 3]
>>> spam = spam + ['A', 'B', 'C']
>>> spam
[1, 2, 3, 'A', 'B', 'C']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)