color_names = ['orange', 'yellow', 'green']
color_names.insert(0, 'red')
color_names
color_names.append('blue')
color_names
+=.color_names.extend(['indigo', 'violet'])
color_names
sample_list = []
s = 'abc'
sample_list.extend(s)
sample_list
t = (1, 2, 3)
sample_list.extend(t)
sample_list
extend expects one iterable argument.sample_list.extend((4, 5, 6)) # note the extra parentheses
sample_list
ValueError occurs if remove’s argument is not in the list.color_names.remove('green')
color_names
color_names.clear()
color_names
responses = [1, 2, 5, 4, 3, 5, 2, 1, 3, 3,
1, 4, 3, 3, 3, 2, 3, 3, 2, 2]
for i in range(1, 6):
print(f'{i} appears {responses.count(i)} times in responses')
reverse reverses the contents of a list in place.color_names = ['red', 'orange', 'yellow', 'green', 'blue']
color_names.reverse()
color_names
copy returns a new list containing a shallow copy.copied_list = color_names.copy()
copied_list
©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.