Fatima Ali
Dev tips

Follow

Dev tips

Follow
Working with lists in Python

Working with lists in Python

Fatima Ali's photo
Fatima Ali
·Jan 3, 2023·

4 min read

A list in Python is a collection of items in a particular order, used to collect multiple units into a single variable making it easier to access and work with all together. A list is represented by square brackets - '[]' and contains comma-separated values which can be accessed individually.

Now, you may be wondering what the point is in using lists when arrays are much faster. Well, that is because the elements stored in an array are homogeneous i.e. they have the same datatype, whereas a list can contain all sorts of data regardless of its data type, which makes it much more efficient to group certain information together.

~ Accessing elements in a list

Since lists are collections of items, you can access any element in a list by telling python its position or index. Indexing always begins at 0. This is because an index is simply the distance of an element from the starting point. Since the first element is positioned at the start, the distance becomes 0 and therefore the index becomes 0. Take a look at the following code sample :

new_list = ['Jake', 'Amy', 'Charles'] #note the square brackets
print(new_list)

On running this, we get an output that looks something like this :

['Jake', 'Amy', 'Charles']

As you can see, Python returns its representation of a list, including the square brackets. To remove these and get the actual values, you can print each individual item in the list using its index.

new_list = ['Jake',  'Amy',  'Charles']             
print(new_list[0])

Since the element at index zero, that is the first element, is 'Jake', the output produced will be Jake.

If you don't want to manually go on printing the entire contents of a list by this method, you can use a for loop to iterate over each element and consequently print it.

new_list = ['Jake',  'Amy',  'Charles']
for name in new_list:
    print(name)

The output produced would look something like this:

Jake
Amy
Charles

~ List functions

  1. Adding Elements to a list

    a. To add elements to the end of a list, you can use the append() function.

     orig_list = ['Jake',  'Amy',  'Charles'] 
     new_list = ['Jake',  'Amy',  'Charles']   
     new_list.append('Rosa')
     print(orig_list)
     print(new_list)
    

    The output would look something like this :

    ['Jake', 'Amy', 'Charles'] ['Jake', 'Amy', 'Charles', 'Rosa']

b. You can also insert an element into a list at any given position using the .insert() function.

orig_list = ['Jake',  'Amy',  'Charles'] 
new_list = ['Jake',  'Amy',  'Charles']   
new_list.insert(0,'Rosa')
print(orig_list)
print(new_list)

The output would be :

['Jake', 'Amy', 'Charles'] ['Rosa', 'Jake', 'Amy', 'Charles']

As you can see, the insert function opens up a space at index zero (as mentioned above) which is then filled up by 'Rosa', the element inserted into the list.

  1. Removing Elements from a list

    a. Removing an element from a certain position

    For this purpose, you can use the del statement.

     orig_list = ['Jake',  'Amy',  'Charles'] 
     new_list = ['Jake',  'Amy',  'Charles']   
     del new_list[0]
     print(orig_list)
     print(new_list)
    

The above code removes the element at index 0, which is Jake, leaving us with ['Amy', 'Charles'].

b. Removing an element and re-using it
You can remove a certain element from a list using the pop() method and store the deleted element in a variable so that it can be reused.

orig_list = ['Jake',  'Amy',  'Charles'] 
new_list = ['Jake',  'Amy',  'Charles']   
popped = new_list.pop()
print(orig_list)
print(new_list)

By default, the element popped is that of the last index. However, you can pop an element off of any index by specifying it in between the brackets of pop() like : new_list.pop(0)

c. Removing an element without knowing the index
If you want to remove an element from a list but do not know anything but its value, you can use the remove() function.

orig_list = ['Jake',  'Amy',  'Charles'] 
new_list = ['Jake',  'Amy',  'Charles']   
new_list.remove('Amy')
print(orig_list)
print(new_list)

The code above, quite obviously, removes Amy from the list new_list.

Other common list methods include :

  1. Sort() -

    list.sort() is used to sort a list alphabetically.

  2. Reverse() -

    list.reverse() is used to reverse a list.

  3. len() -

    len(list) is used to get the length of a string which might be useful if you want to create a loop.

And that was an introduction to lists in Python! Hope you learned something new and thanks for sticking around till the end.

 
Share this