top of page

Methods for list

.join(); .index(); .append(); .insert(); .remove(); .sort(); .sorted(); .reverse(); .copy(); .deepcopy(); .pop(); .count(); .zip(); .zip(*iterables); 

.join(): useful when you have a list of strings that need to be joined together into a single string value
input: list

ouput: string

>>> ', '.join(['tiger', 'sloth', 'fatcat'])

'tiger, sloth, fatcat'

>>> ' '.join(['My', 'name', 'is', 'Tiger'])

'My name is Tiger'

>>> 'ABC'.join(['My', 'name', 'is', 'Tiger'])

'MyABCnameABCisABCTiger'

.index()

input: list

output: int

E.g.1.

>>> spam = ['hello', 'world']

>>> spam.index('hello')

0

>>> spam.index('hello hello hello')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: 'hello hello hello' is not in list

 

.append()

input: list

output: list

E.g.1

>>> spam = ['cat', 'dog', 'bat']

>>> spam.append('moose')

>>> spam

['cat', 'dog', 'bat', 'moose']

.insert()

input: list

output: list

E.g.1

>>> spam = ['cat', 'dog', 'bat']

>>> spam.insert(1, 'chicken')

>>> spam

['cat', 'chicken', 'dog', 'bat']

.remove() - del statement is good to use when you know the index of the value you want to remove. The remove() method is good when you know the value you want to remove from the list.

input: list

output: list

>>> spam = ['cat', 'bat', 'rat', 'elephant']

>>> spam.remove('bat')

>>> spam 

['cat', 'rat', 'elephant']

 

V.S.

 

>>> spam = ['cat', 'bat', 'rat', 'elephant']

>>> del spam[2]

>>> spam

['cat', 'bat', 'elephant']

>>>

.sort() - permanently changing the original list order; compare this with the function sorted()

input: list

output: list

E.g.1.

>>> spam = [2, 5, 3.14, 1, -7]

>>> spam.sort()

>>> spam

[-7, 1, 2, 3.14, 5]

 

>>> spam.sort(reverse=True)

>>> spam

[5, 3.14, 2, 1, -7]

 

E.g.2. true alphabetical order instead of default asciibetical order

 

Asciibetical order: uppercase letters come before lowercase letters

>>> spam = ['Alice', 'ants', 'Bob', 'badgers', 'Carol', 'cats']

>>> spam.sort()

>>> spam

['Alice', 'Bob', 'Carol', 'ants', 'badgers', 'cats']

 

Regular alphabetical order:

>>> spam.sort(key=str.lower)

>>> spam

['Alice', 'ants', 'badgers', 'Bob', 'Carol', 'cats']

 

Sorted function: (kept the original order)

E.g.1.

>>> cars = ['bmw', 'audi', 'toyota', 'subaru']

>>> print(sorted(cars))

['audi', 'bmw', 'subaru', 'toyota']

>>> print(cars)

['bmw', 'audi', 'toyota', 'subaru']

 

.reverse() - printing a List in Reverse Order

input: list

output: list

E.g.1

>>> cars = ['bmw', 'audi', 'toyota', 'subaru']

>>> cars.reverse()

>>> print(cars)

['subaru', 'toyota', 'audi', 'bmw']

copy.copy() 

input: list

output: list

E.g.1.

>>> import copy
>>> spam = ['A', 'B', 'C', 'D']
>>> id(spam)
44684131
>>> cheese = copy.copy(spam)
>>> id(cheese) # cheese is a different list with different identity.
44685832
>>> cheese[1] = 42
>>> spam
['A', 'B', 'C', 'D']
>>> cheese
['A', 42, 'C', 'D']

Notice both spam and cheese have different IDs, that means they both pointing to different references. List is a mutable data type, however, copy.copy() actually creates a completely different list.

copy.deepcopy() - if the list you need to copy contains lists, then use the copy.deepcopy() function instead of copy.copy()

input: list

output: list

.pop(): removes the last item in a list, but it lets you work

with that item after removing it, and the popped item will be no longer existing in the list

input: list 

output: string

E.g.1.

motorcycles = ['honda', 'yamaha', 'suzuki']

last_owned = motorcycles.pop()

print("The last motorcycle I owned was a " + last_owned.title() + ".")

The last motorcycle I owned was a Suzuki.

 

E.g.2

motorcycles = ['honda', 'yamaha', 'suzuki']

second_owned = motorcycles.pop(1)

print("The 2nd motorcycle I owned was a " + second_owned.title() + ".")

The 2nd motorcycle I owned was a Yamaha.

.count()

input: list

output: int 

>>> li = ['hello', 'world', 'apple']
>>> li.count('hello')
1

.zip()

input: list

output: list of paired tuples 

>>> x = [1, 2, 3]

>>> y = [4, 5, 6]

>>> zipped = zip(x, y)

>>> list(zipped)

[(1, 4), (2, 5), (3, 6)]

>>> x2, y2 = zip(*zip(x, y))           # zip() in conjunction with the * operator can be used to unzip a list

>>> x == list(x2) and y == list(y2)

True

.zip(*iterables)

input: list

output: list of paired tuples

>>> grid = [['A', 'B', 'C', 'D'], [1, 2, 3, 4]]
>>> new_grid_obj = zip(*grid)
>>> print(list(new_grid_obj))

[('A', 1), ('B', 2), ('C', 3), ('D', 4)]

bottom of page