5.10 Other List Methods

In [1]:
color_names = ['orange', 'yellow', 'green']

Inserting an Element at a Specific List Index

In [2]:
color_names.insert(0, 'red')
In [3]:
color_names
Out[3]:
['red', 'orange', 'yellow', 'green']

Adding an Element to the End of a List

In [4]:
color_names.append('blue')
In [5]:
color_names
Out[5]:
['red', 'orange', 'yellow', 'green', 'blue']

Adding All the Elements of a Sequence to the End of a List

  • Equivalent to +=.
In [6]:
color_names.extend(['indigo', 'violet'])
In [7]:
color_names
Out[7]:
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
In [8]:
sample_list = []
In [9]:
s = 'abc'
In [10]:
sample_list.extend(s)
In [11]:
sample_list
Out[11]:
['a', 'b', 'c']
In [12]:
t = (1, 2, 3)
In [13]:
sample_list.extend(t)
In [14]:
sample_list
Out[14]:
['a', 'b', 'c', 1, 2, 3]
  • Parentheses are required for the tuple argument below, because extend expects one iterable argument.
In [15]:
sample_list.extend((4, 5, 6))  # note the extra parentheses
In [16]:
sample_list
Out[16]:
['a', 'b', 'c', 1, 2, 3, 4, 5, 6]

Removing the First Occurrence of an Element in a List

  • ValueError occurs if remove’s argument is not in the list.
In [17]:
color_names.remove('green')
In [18]:
color_names
Out[18]:
['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']

Emptying a List

In [19]:
color_names.clear()
In [20]:
color_names
Out[20]:
[]

Counting the Number of Occurrences of an Item

In [21]:
responses = [1, 2, 5, 4, 3, 5, 2, 1, 3, 3, 
             1, 4, 3, 3, 3, 2, 3, 3, 2, 2]
In [22]:
for i in range(1, 6):
    print(f'{i} appears {responses.count(i)} times in responses')
1 appears 3 times in responses
2 appears 5 times in responses
3 appears 8 times in responses
4 appears 2 times in responses
5 appears 2 times in responses

Reversing a List’s Elements

  • Method reverse reverses the contents of a list in place.
In [23]:
color_names = ['red', 'orange', 'yellow', 'green', 'blue']
In [24]:
color_names.reverse()
In [25]:
color_names
Out[25]:
['blue', 'green', 'yellow', 'orange', 'red']

Copying a List

  • Method copy returns a new list containing a shallow copy.
In [26]:
copied_list = color_names.copy()
In [27]:
copied_list
Out[27]:
['blue', 'green', 'yellow', 'orange', 'red']

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.