List in python is mutable types which means it can be changed after assigning some value. The list is similar to arrays in other programming languages. In this article, we are going to see how to change list items in python. Show
Let’s understand first how to access elements in python:
Python3
Output: 10 20 60 Now we can change the item list with a different method:Example 1: Change Single list item. Approach:
Code: Python3
Output: original list [10, 20, 30, 40, 50, 60] New list [11, 21, 30, 40, 50, 61] Example 2: Changing all values using loops. Python3
Output: Original list [10, 20, 30, 40, 50, 60] After incrementing each element of list by 2 [12, 22, 32, 42, 52, 62] Example 3: Changing all values of a list using List comprehension. Python3
Output: Original list [10, 20, 30, 40, 50] After incrementing each element of list by 2 [12, 22, 32, 42, 52] Python Lists and List Manipulation Video Before starting, I should mention that the code in this blog post and in the video above is available on my github. Defining a ListLists are written within square brackets [] Defining a List. The second row in this table index is how you access items in the list.# Define a list Lists store an ordered collection of items which can be of different types. The list defined above has items that are all of the same type (int), but all the items of a list do not need to be of the same type as you can see below. # Define a list The list contains an int, a bool, a string, and a float. Access Values in a ListEach item in a list has an assigned index value. It is important to note that python is a zero indexed based language. All this means is that the first item in the list is at index 0. Access item at index 0 (in blue)# Define a listOutput of accessing the item at index 0. Python also supports negative indexing. Negative indexing starts from the end. It can be more convienient at times to use negative indexing to get the last item in the list because you don’t have to know the length of the list to access the last item. Accessing the item at the last index.# print last item in the list Output of accessing the last item in the ListAs a reminder, you could also access the same item using positive indexes (as seen below). Alternative way of accessing the last item in the list zSlice of ListsSlices are good for getting a subset of values in your list. For the example code below, it will return a list with the items from index 0 up to and not including index 2. First index is inclusive (before the :) and last (after the :) is not# Define a listSlice of a list syntax. # everything up to but not including index 3 The code below returns a list with items from index 1 to the end of the list # index 1 to end of list Update Item in a ListLists in Python are mutable. All that means is that after defining a list, it is possible to update the individual items in a list. # Defining a listCode to modify an item in a list List MethodsPython lists have different methods that help you modify a list. This section of the tutorial just goes over various python list methods. Index Method# Define a list The index method returns the first index at which a value occurs. In the code below, it will return 0. print(z.index(4)) You can also specify where you start your search. print(z.index(4, 3)) Count MethodThe count method works just like how it sounds. It counts the number of times a value occurs in a list random_list = [4, 1, 5, 4, 10, 4] Sort MethodSort a Python List - the actual code would be: z.sort()The sort method sorts and alters the original list in place. z = [3, 7, 4, 2] The code above sorts a list from low to high. The code below shows that you can also sort a list from high to low. # Sorting and Altering original list As an aside, I should mention that you can also sort a list of strings from a-z and z-a as you can see here. Append MethodAdd the value 3 to the end of the list.The append method adds an element to the end of a list. This happens inplace. z = [7, 4, 3, 2] Remove MethodThe remove method removes the first occurrence of a value in a list. z = [7, 4, 3, 2, 3] Code removes the first occurrence of the value 2 from the list zPop Methodz.pop(1) removes the value at index 1 and returns the value 4.The pop method removes an item at the index you provide. This method will also return the item you removed from the list. If you don’t provide an index, it will by default remove the item at the last index. z = [7, 4, 3, 3] Extend MethodThe method extends a list by appending items. The benefit of this is you can add lists together. z = [7, 3, 3] Add the list [4, 5] to the end of the list z.Alternatively, the same thing could be accomplished by using the + operator. print([1,2] + [3,4]) Insert Methodinsert the list [1,2] at index 4The insert method inserts an item before the index you provide z = [7, 3, 3, 4, 5] Closing RemarksPlease let me know if you have any questions either here or in the comments section of the youtube video or through Twitter! Next post reviews python dictionaries. If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning course. Here is a free preview video. How do you update a list in Python?Different ways to update Python List:. list.append(value) # Append a value.. list.extend(iterable) # Append a series of values.. list.insert(index, value) # At index, insert value.. list.remove(value) # Remove first instance of value.. list.clear() # Remove all elements.. How do I update a nested list in Python?To add new values to the end of the nested list, use append() method. When you want to insert an item at a specific position in a nested list, use insert() method. You can merge one list into another by using extend() method.
How do you overwrite a list in Python?We can also use a while loop to replace values in the list. While loop does the same work as for loop. In the while loop first, we define a variable with value 0 and iterate over the list. If value matches to value that we want to replace then we replace it with the new value.
How do I make a list inside a list in Python?# creating list. nestedList = [1, 2, ['a', 1], 3] . # indexing list: the sublist has now been accessed. subList = nestedList[2] . # access the first element inside the inner list: element = nestedList[2][0] . |