7 Essential Things in a Python List

onestop_databy:

Python

This short tutorial will teach you how to use a list in Python (not a Python array – it is not supported). It will focus on a list in list, a string to list, remove from list, index item (find), add to list (append), sort list, etc.

What is a List?

A list is a collection that is ordered and changeable. In Python, lists are defined with square brackets as exemplified below:

$ my_list = []

1. Append and insert – Adding an element to a list

In Python, you can add an item to the end of the list using the method append.

>>> my_list = []
>>> # add 1 to list
>>> my_list.append(1)
>>> my_list
>>> [1]

If you want to add an element to the beginning of the list in Python or to a specific index, you can use the method insert.

>>> my_list = [2,3]
>>> # add "NEW_ITEM" to position 0
>>> my_list.insert(0,"NEW_ITEM")
>>> my_list
>>> ['NEW_ITEM', 2, 3]

2. In – Checking if an element is in a list

It is very easy to check if an item is on the list by using “in”. For example:

>>> my_list = ['NEW_ITEM', 2, 3]
>>> check = 2 in my_list
>>> check
>>> True
>>> check_2 = 5 in my_list
>>> check_2
>>> False

Moreover, you can also use “is not” which checks if the item IS NOT in the list.

3. Remove – Remove an element from a list

You can use remove an element from a list by using the method remove().

>>> my_list = [1, 2, 3]
>>> my_list.remove(2)
>>> my_list
>>> [1, 3]

Most of the time, you are not sure if the element is in the list before you wanting to remove it. Thus, check if the element is in the list before removing it. For example:

>>> my_list = [1, 2, 3]
>>> # check if 2 is in the list before trying to delete it
>>> if 2 in my_list:my_list.remove(2)
>>> my_list
>>> [1, 3]

4. Index – Finding an element position in a list

You can find the index of an element in your list by using the method index().

>>> my_list = [1, 2, 3]
>>> # try to find index for `1` in list
>>> my_list.index(1)
>>> 0

Again, if you are not sure the item is in the list, you should check before trying to call the index method

>>> look_for_index = 1
>>> my_list.index(look_for_index) if look_for_index in my_list else None
>>> 0

Moreover, you can use the index to retrieve the element from the list.

>>> my_list = [1, 2, 3]
>>> my_list[0]
>>> 1
>>> my_list[1]
>>> 2
>>> my_list[2]
>>> 3

Hot Tip: If you want to access the LAST element in your list you can use the “-1” indexing

>>> my_list = [1, 2, 3]
>>> my_list[-1]
>>> 3

5. string to list – Converting a string to a list

You can easily convert a string into a list of strings assuming you have a delimiter in the string. All you need to do is to call the method split(delimiter) and it will split the string into a list of strings. on the split method, the blank space is the default for the delimiter.

>>> my_string = "One Stop For Data Analysis is Awesome"
>>> ['One', 'Stop', 'For', 'Data', 'Analysis', 'is', 'Awesome']

If you wanted the opposite (python list to string), you could simply do the following:

>>> my_list = ['One', 'Stop', 'For', 'Data', 'Analysis', 'is', 'Awesome']
>>> " ".join(my_list)
>>> # Join the list into a single string by the delimeter " "
>>> "One Stop For Data Analysis is Awesome"

Attention: If the list contains an element that is not string type, you will need to use list comprehension to convert it to string type before joining the list into a string – as done below:

>>> my_list = ['One', 'Stop', 'For', 'Data', 'Analysis', 'is', 'Awesome', 1000]
>>> # convert all elements to string
>>> my_list = [str(x) for x in my_list]
>>> " ".join(my_list)
>>> # Join the list into a single string by the delimeter " "
>>> "One Stop For Data Analysis is Awesome 1000"

6. sort – Sorting a list

You can sort a list of items by calling the method sort().

>>> my_list = [2, 1, 3]
>>> my_list.sort(1)
>>> my_list
>>> [1, 2, 3]

You can add the parameter reverse=False to the method, in case you want to get a reverse sorted list.

7. List in List – have a list of lists

In Python, you can mix and match what kind of items you will have in the list. For example, you could have a list of integers as showed before or a list of lists as showed below.

>>> my_list = []
>>> sub_list_1 = [1, 2, 3] 
>>> sub_list_2 = [4, 5] 
>>> sub_list_3 = [6] 
>>> my_list.append(sub_list_1)
>>> my_list.append(sub_list_2)
>>> my_list.append(sub_list_3)
>>> my_list
>>> [[1, 2, 3], [4, 5],[6]]

Or even a list of different types such as a set and dictionary, float, int, tuple, etc.

>>> my_list = []
>>> sub_item_1 = [1, 2, 3] 
>>> sub_item_2 = 1
>>> sub_item_3 = {1:10}
>>> my_list.append(sub_item_1)
>>> my_list.append(sub_item_2)
>>> my_list.append(sub_item_3)
>>> my_list
>>> [[1, 2, 3], 1, {1:10}]

More Resources

Here are three of my favorite Python Books in case you want to learn more about it.

Related Posts