Joining arrays and strings

ARRAY

1. Append

  • to add only one element or any object at the end of the list

Note: The length of the list increases by one.

Example 1.1:

a = [1, 'x', 2]
a.append('y')
print(a)

OUTPUT:
[1, 'x', 2, 'y']

Example 1.2:

a = [1, 'x', 2]
b = ['y', 3, 'z']
a.append(b)
print(a)

OUTPUT:
[1, 'x', 2, ['y', 3, 'z']]

Notice that 'b' is added as a single object at the end of the list 'a'

2. Extend

  • to combine elements of another iterable to a list

Note: The length of the list increases by the number of elements in its arguments.
Caution: Unlike append, extend takes only iterable as an arguments.

Example 2.1:

a = [1, 'x', 2]
b = ['y', 3, 'z']
a.extend(b)
print(a)

OUTPUT:
[1, 'x', 2, 'y', 3, 'z']

Notice that each element of 'b' is added separately to the list 'a'

Example 2.2:

a = [1,'x', 2]

a.extend(3)

OUTPUT:
TypeError: 'int' object is not iterable

Notice that we can't add non iterable

Example 2.3:

a = [1,'x', 2]
b = 'hello'
a.extend(b)
print(a)

OUTPUT:
[1, 'x', 2, 'h', 'e', 'l', 'l', 'o']

Since string is an iterable, each character of the string gets added as a separate element to 'a'

Side Note: How is ‘EXTEND’ different from ‘+’ operator

a = [1, 'x', 2]
b = ['y', 3, 'z']
a + b

OUTPUT:
[1, 'x', 2, 'y', 3, 'z']

We can produce the same result as _Example 2.1_ using '+' operator as well but there are two major differences between 'EXTEND' and '+'

(i) We can't concatenate any other iterable (like string, tuple, dictionary) except list to a list using '+' operator

a = [1, 'x', 2]
b = 'hello'
a + b

OUTPUT:
TypeError: can only concatenate list (not "str") to list

(ii) '+' operator creates a new list whereas 'EXTEND' does _in-place_ modification. This makes '+' computationally expensive operation compared to 'EXTEND'

a = [1, 'x', 2]
b = ['y', 3, 'z']
c = a + b
print(a)
print(b)
print(c)

OUTPUT:
[1, 'x', 2]
['y', 3, 'z']
[1, 'x', 2, 'y', 3, 'z']

3. INSERT

  • to add an element/object at some particular index

Example 3.1

a = [1, 'x', 5]
a.insert(2,4)

OUTPUT:
[1, 'x', 4, 5]

First argument is ‘index’ whereas second one is ‘item’

STRING

1. Use ‘+’ operator to append two separate strings

Example 1.1

a = 'hello'
b = ' world'
print(a + b)

OUTPUT:
hello world

2. f-string

Instead of using ‘+’, a new type of string formatting in Python 3.6+ can also be used for concatenating strings.

Example 2.1

url = "https://www2.census.gov/geo/tiger/GENZ2017/shp/"
file = "cb_2017_02_tract_500k.zip"
fullpath = f'{url}{file}'
print(fullpath)

OUTPUT:
'https://www2.census.gov/geo/tiger/GENZ2017/shp/cb_2017_02_tract_500k.zip'

For more information on f-string, check Efficient way of string formatting: Python 3’s f-string

3. Use ‘join’ to concatenate list of strings

Example 3.1

  .join([hello, world])

OUTPUT:
hello world

Example 3.2

, .join([hello, world])

OUTPUT:
hello,world

Notice that the object before the join acts as a separator. In Example 3.1, ‘space’ acts as a separator whereas in Example 3.2, ‘comma’ acts as separator

SUMMARY

FOR ARRAY:

append: to add only one element or an object at the end of the list
insert: to add an element/object at some particular index
extend: to combine elements of another iterable to a list.

’+’ is relatively heavy operation when dealing with list concatenation

FOR STRING:

’+’: for adding two/more simple objects
‘f-string’: new and efficient way of formatting and concatenating string objects
‘join’: for list of string objects