Monday, October 8, 2018

Organizing a List

Python provides a number of different ways to organize your lists, depending on the situation as our lists will be created in an unpredictable order, because we can’t always control the order in which the users provide their data. This may be unavoidable in most circumstances, but  frequently  we may want to present the information in a desired order. Often we will prefer to preserve the original order of our list, and other times we’ll want to change the original order.

Here are some of the methods to organize lists:

1. Sorting list with sort()

Using the sort() we can sort a python list permanently. Let's sort our motorbikes list, we want to change the order of the list to store them alphabetically.

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']

motorbikes.sort()

print(motorbikes)

The sort() method, changes the order of the list permanently. The cars are now in alphabetical order, and we can never revert to the original order. See the screen shot below for the program and it's output:



The sort () can be used to sort motorbikes list in reverse alphabetical order by passing the argument reverse=True to the sort() method.

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']

motorbikes.sort(reverse=True)

print(motorbikes)

See the screen shot below for the program and it's output:


 


 2. Sorting list with sorted()

The sort() method, changes the order of the list permanently however sometimes we may require to a sorted list but maintain it's original order. In such cases we should sort out list using sorted() lets you display your list in a particular order but doesn’t affect the actual order of the list.

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']

sorted(motorbikes)

print(motorbikes)


See the screen shot below for the program and it's output:


We first print the list in its original order and then in alphabetical order. After the list is displayed in the new order, we can see that the list is still stored in its original order after the sorted() function has been used.

The sorted() function, like sort() can also accept a reverse=True argument if we want to display a list in reverse alphabetical order.



motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']

print(sorted(motorbikes, reverse = True))

print(motorbikes)

See the screen shot below for the program and it's output:


 3. Reversing a list with reverse()

If originally the list items (motorbikes list) are stored in chronological order according to when we owned them, we could easily rearrange the list into reverse chronological order using the reverse().

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
motorbikes.reverse()
motorbikes.reverse()

See the screen shot below for the program and it's output:



As you can see in the output, reverse() doesn’t sort backward alphabetically; it simply reverses the order of the list. The reverse() method changes the order of a list permanently, but we can revert to the original order anytime by applying reverse() to the same list a second time.

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
motorbikes.reverse()
motorbikes.reverse()

See the screen shot below for the program and it's output:


  4. Finding the length of a list with len()

The length of a list can be calculated by using the len() function. This can be useful when you need to identify the number of enemies that still need to be shot down in a game, determine the amount of data you have to manage in a visualization, or figure out the number of registered users on a website, among other tasks.

len (motorbikes)

See the screen shot below for the program and it's output:





We have stored the length of list in a variable number_of_bikes as shown below and then printed this variable which gave us the length of the list.

number_of_bikes = len (motorbikes)

5. Index error in lists

A very common mistake happens when we start working with the lists in the beginning which results in an index error. Our motorbikes list has 5 items as shown below -

motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']

Now let's try to execute :

print(motorbikes[5])

Here we intend to access and print the fifth item 'Harley Davidson'. See the screen shot below for the program and it's output:




 This program results in an index error. Python attempts to give you the item at index 5. But when it searches the list, no item in motorcycles has an index of 5. Because of the off-by-one nature of indexing in lists, this error is typical. People think the fifth item is item number 5, because they start counting at 1. But in Python the fifth item is number 4, because it starts indexing at 0.

An index error means Python can’t figure out the index you requested. If an index error occurs in your program, try adjusting the index you’re asking for by one. Then run the program again to see if the results are correct. Keep in mind that whenever you want to access the last item in a list you use the index -1. This will always work, even if your list has changed size since the last time you accessed it.

print(motorbikes[-1]) 


Here we intend to access and print the last item 'Harley Davidson'. See the screen shot below for the program and it's output:


In this post we have learned to sort lists permanently and temporarily for display purposes. We also learned how to find the length of a list and how to avoid index errors when you’re working with lists. In the next post we'll learn how to work with items in a list more efficiently.

Till then make some more programs and use the methods we learned. Keep practicing and remember Python is easy to learn!


Share:

0 comments:

Post a Comment