Estimated reading time: 2 minutes
Python lists are used extensively in projects, as a result it is important to understand their structure.
Some of the things they can be used for:
- Lookup values for comparisons.
- Passing data to them to store to be referenced elsewhere.
- As part of a loop, store values that have been found through the loop logic.
With methods are associated with lists?
- Append – Add values to the end of the list
- Extend – adds values from an iterable object to the end of the list.
- Insert – You can insert an item to a certain position in a list.
- Remove – Remove the first value in a list that has a value that was asked to be looked for.
- Pop – This also removes a value at a certain position and returns, consequently if no position is specified then it removes the last item and returns it.
- Clear – Removes all items from the list.
- Index – returns the index value of the first item found that was asked to be searched for.
- Count – returns the number of times an item that was searched or was found in a list.
- Sort – Sorts the items in the list.
- Reverse – This reverses the items in the list.
- Copy – This makes a copy of the list.
What are the properties of a list?
The data type has the following attributes, that make it really useful for a vast array of scenarios:
- They are ordered – Whatever order the list is a unique characteristic of the list, furthermore changing the order makes it a different list.
- You can use their index to access the value.
- They are mutable, meaning you can apply any of the above methods on them.
- They can contain strings, integers etc, accordingly, there is no restriction on what can be in the list.
Check out the below video playlist from our YouTube channel, they will help explain more about lists:
On this website you can also read about how to compare two lists in Python or how to sort lists using rstudio in addition to this blog post.
We hope you enjoy it!
Data Analytics Ireland