Lists in Python

Sequences are a basic data structure in Python that are used to group data together. Sequences can also be called Collections in Python. Data is labelled and accessed using indexes that mark their location. 

Lists are the most commonly used collections in Python. A list is a sequence of objects which is ordered and changeable. In this article, we will be learning all about lists in Python.

Creating Lists

Lists are denoted by square brackets in Python. To create a list, you just have to write comma-separated values and store it in a variable. The interesting thing about lists is that you can put elements of mixed types in them. 

myList = [“Pakistan”, “Berlin”, 1947, 22, 0.5]
print(myList)

When the above code is executed, it produces the following result:

[“Pakistan”, “Berlin”, 1947, 22, 0.5]

Indexing and Accessing Values in Lists

To access values of an array, square brackets are used with the index of the value that you are trying to access. Considering the above list, we can access its values as:

print(“Second element: “, myList[1])
print(“Fourth element: “, myList[3])

When the above code is executed, it produces the following result:

Berlin
22

Some things to note here:

  • List indices start from 0, not 1.
  • You can also access the list elements of a list by -1, second last by -2, and so on.

Slicing Lists

To access a range of values, we use slicing of arrays. It is done by providing the starting and ending index separated by a colon (:) inside the square brackets.

print(“Second to Fourth Element: ”, myList[1:3])

When the above code is executed, it produces the following result:

[Berlin, 1947, 22]

Updating Lists

Whether you want to add new elements to a list or update the existing ones, Python provides simple operations for both functions.

Changing Existing Elements

To change or update and existing element, you simply have to assign the new value to its index using the assignment operator.

myList[2] = “Moscow”
print(myList)

When the above code is executed, it produces the following result:

[“Pakistan”, “Moscow”, 1947, 22, 0.5]

Adding Elements

To add elements to the end of a list, you can use the append method available for lists:

myList.append(2005)
print(myList)

When the above code is executed, it produces the following result:

[“Pakistan”, “Moscow”, 1947, 22, 0.5, 2005]

However, if you want to add elements at a specific index of a list, insert method is used. It takes the index and value as its arguments.

myList.insert(1, 24.7)
print(myList)

When the above code is executed, it produces the following result:

[“Pakistan”, 24.7, “Moscow”, 1947, 22, 0.5, 2005]

Deleting Lists and List Values

If you want to empty a list without losing the list itself, you can use the clear method:

myList.clear()
print(myList)

When the above code is executed, it produces the following result:

[]

But if you want to delete a value at a specific index of a list, you can use del keyword:

del myList[5]
print(myList)

del myList #deletes the whole list

When the above code is executed, it produces the following result:

[“Pakistan”, 24.7, “Moscow”, 1947, 22, 2005]

However, if you don’t know where your specific element is in the list, you can use remove(). It searches for the value and deletes it from the list.

myList.remove(22)
print(myList)

When the above code is executed, it produces the following result:

[“Pakistan”, 24.7, “Moscow”, 1947, 2005]

Concatenating Two Lists

If you want to join two lists, its as simple as adding them using ‘+’ operator in Python.

list1 = [56, 34, 78]
list2 = [0.5, 0.9, 1.5]
list3 = [24, 87, “Russia”] + list2

print(“list1 + list2: ”, list1 + list2)
print(“list3: ”, list3)

When the above code is executed, it produces the following result:

list1 + list2 = [56, 34, 78, 0.5, 0.9, 1.5]
list3 = [24, 87, “Russia”, 0.5, 0.9, 1.5]

This was all about the creation and performing different operations on lists. If you have any questions regarding the functions, do ask in the comments below.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest