![]() Python documentation, chapter "Sequence Types".Use a list comprehension to construct the list.Copy the above list and add '2a' back into the list such that the original is still missing it.Simultaneously remove the element '2a' from the above list and print it.Use a slice on the above list to construct the list. ![]() For example:Ī = b = dif = print () # Exercises To make a static list of items, write them between square brackets. The first is through assignment ("statically"), the second is using list comprehensions ("actively"). There are two different ways to make a list in Python. remove ( "cat" ) # Remove AKA delete #list1.remove("elephant") - throws an error for item in list1 : # Iteration AKA for each item print ( item ) print ( "Item count:", len ( list1 )) # Length AKA size AKA item count list3 = for i in range ( 0, len ( list3 )): # Read-write iteration AKA for each item list3 += 1 # Item access AKA element access by index last = list3 # Last item nextToLast = list3 # Next-to-last item isempty = len ( list3 ) = 0 # Test for emptiness set1 = set () # Initialize set from a list list4 = list ( set1 ) # Get a list from a set list5 = list4 # A shallow list copy list4equal5 = list4 = list5 # True: same by value list4refEqual5 = list4 is list5 # False: not same by reference list6 = list4 del list6 # Clear AKA empty AKA erase list7 = + # Concatenation print ( list1, list2, list3, list4, list5, list6, list7 ) print ( list4equal5, list4refEqual5 ) print ( list3, list3, list3 ) # Slices print ( max ( list3 ), min ( list3 ), sum ( list3 )) # Aggregates print () # List comprehension print () print () print () print () List creation pop ( 1 ) # Remove item at index if "cat" in list1 : # Membership test list1. insert ( 0, "fly" ) # Insert at the beginning list1 = # Add members at the beginning doe = list1. append ( "cat" ) # Add a single member, at the end of the list list1. If you think that you've fixed the problem but the error persists, make sure that you've saved the script file and rerun your code afterwards.List1 = # A new empty list list2 = # A new non-empty list with mixed item types list1. Take your time to read the error messages when they are printied to the screen, they will most likely help you solve the issue. This, along with the line above that which gives the file name and line number within that file, are essential in larger scripts to track down where the problem came from. Moving to the line above that, we see printed the line of code, copied from our script, at which the exception occured. The second part of that line is usually a slightly more descriptive message, in this case telling us that the specific problem was that the index was "out of range", i.e. The first is the word before the colon which tells you the type of the exception is an Inde圎rror, i.e. The last line is Inde圎rror: list index out of range which has two parts to it. ![]() ![]() I always start by reading the last line of an error as that is usually where the most useful information is. Which a very dense collection of information. Running this you will see the following printed to the screen: Traceback (most recent call last):įile "/home/matt/tmp/beginning_python/list.py", line 3, in
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |