Python lists are a fundamental data type in Python. . You can learn more about the main . They are similar to Arrays in other languages, such as Javascript Python data structures here Creating a list in Python is super easy. Let's start with some of the basics, and then dive into useful ways you'll be able to work with lists. To start a new list, simply define a variable in Python, with the contents encapsulated in square brackets: [] myList = [ "some", "list", "contents" ] As Python is a language that is dynamically typed, there are no major limitations on what can go inside a list, so feel free to add other types such as integers or floats too: myList = [ "some", "list", "contents", 1, 2.5, "etc" ] Empty lists can be defined as empty square brackets should you need to do that too: [] myList = [] can also be , or contain lists within lists (... within lists). So this is also valid python: Lists nested myList = [ [ "some", "list" ], "inside", "a", "list" ] Getting the length of a list is done using the standalone function. Here's an example where we try to get the length of our nested list: len() myList = [ [ "some", "list" ], "inside", "a", "list" ] print(len(myList)) # returns 4 Finally, we can reference items in a list using the square bracket notation. For example, to get the first item of a list: myList = [ "some", "list", "contents", 1, 2.5, "etc" ] print(myList[0]) # "some" Or, to get the first two items: myList = [ "some", "list", "contents", 1, 2.5, "etc" ] print(myList[0:2]) # [ "some", "list" ] List Methods in Python come with a bunch of built-in methods in Python, to allow us to fully realize their potential as data stores. These methods are: Lists - appends an item with the value to the end of the list. list.append("newItem") newItem - appends another iterable item onto the list, for example - combining two lists. list.extend(["newItem"]) - inserts a list item and indexes 0. You can change the value of the index, to decide where the item should be inserted. list.insert(0, "newItem") newItem - deletes the contents of the list entirely. list.clear() - removes the first item of the list with a value of . Will throw an error if no value exists. list.remove("someItem") item item - counts any instances of in the list. If no item in the list has the value then it will return 0. list.count("someItem") someItem someItem - creates a shallow copy of the list. list.copy() - reverse the elements of the list. list.reverse() - removes an item at position 10 of the list, and returns it. If you don't define a number, it'll remove the last item, i.e. . list.pop(10) list.pop() - for sorting lists. list.sort() To bring this to life, let's look at a few examples of how it works in practice: Appending and Inserting new list items One of the most common things you'll want to do with a list is adding new data to it. If we've created a list, we can append data using the method as mentioned, or to insert items at a certain position: append insert myList = [ "inside", "a", "list" ] myList.append("friend") myList.insert(2, "new") print(myList); # ['inside', 'a', 'new', 'list', 'friend'] You can also add to a list using : += myList = [ "inside", "a", "list" ] myList += "friend" print(myList) # ['inside', 'a', 'list', 'friend'] Or, if you have two lists, you can add the second onto the first using : extend myList = [ "some", "list" ] otherList = [ "other", "list" ] myList.extend(otherList) print(myList) # ['some', 'list', 'other', 'list'] Deleting lists and lists content An equally common thing you'll want to do, once you add everything to your list, is to delete items. You can delete with or to simply delete the entire list: remove clear myList = [ "inside", "a", "list" ] myList.remove("inside") print(myList) # ['a', 'new'] myList.clear() print(myList) # returns [] Reversing a List in Python You'll also want to reverse lists, in some situations. Python has a built-in method for this, so no need to define your own: myList = [ "inside", "a", "list" ] myList.reverse() print(myList) # ['list', 'a', 'inside'] Making copies of lists In Python, we use to compare by value, and to compare by reference. We can use to make a new reference for a list. This will make a new reference point in memory pointing to the same value. Below, and are equal in value, but now their reference is different, so returns false: == is copy() myList otherList myList is otherList myList = [ "a", "list" ] otherList = myList.copy() print(myList == otherList) # True print(myList is otherList) # False This can also be written as , if you want to avoid using the method: myList[:] copy() myList = [ "a", "list" ] otherList = myList[:] print(myList == otherList) # True print(myList is otherList) # False Sorting Lists in Python Sorting a list in ascending order is easy using the function, and all items are of the same type: sort myList = [ "a", "c", "e", "b", "f", "d", "g", "z", "w", "x" ] myNumberList = [ 1, 3, 5, 2, 7, 4, 6 ] myList.sort() myNumberList.sort() print(myList) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'w', 'x', 'z'] print(myNumberList) # [1, 2, 3, 4, 5, 6, 7] If you try to sort where the list contains different types - like integers and strings, you'll end up getting an error. If you want to sort a list based on another feature of the list, you can define its , and arguments: key reverse gives us a number that will be used to compare the list content key if set to true will reverse the order. reverse For example, to put all values which are at the start, we could try something like this: a def isA(letter): if(letter == "a"): return 1 else: return 0 myList = [ "a", "c", "a", 5, "f", "a", 2, "z", "a", "x" ] myList.sort(key=isA, reverse=True) print(myList) # ['a', 'a', 'a', 'a', 'c', 5, 'f', 2, 'z', 'x'] Here, we define a function , which takes each item in the list as its first argument ( ). If the letter is , then it returns 1, otherwise, it returns 0. Then we reverse the list using the argument, to get all the s at the start. isA letter a reverse=True a Lists function best as stacks of data! Since lists are , they function best as stacks, which means adding and removing items from the end of a list is super fast, while adding or removing items from the start is kind of slow. That means it's recommended to use and where possible, as this is going to be a lot faster on large data sets than other methods on lists. ordered pop append Conclusion Lists are super powerful data structures in Python and are used . You can learn more about Python data structures here. To learn more about other engineering topics, . You can read more about Python data structures below: everywhere check out the rest of my content Python Data Collections Python Data Collections: Lists Python Data Collections: Tuples Python Data Collections: Sets Python Data Collections: Dictionaries Also published . here