StackTips

Python: Working with Lists

stacktips avtar

Written by

Editorial,  8 min read,  2.31K views, updated on Sept. 17, 2023

Lists are used to store collection of items in a single variable. They are very similar to what other programming languages call Arrays.

Items stored in the list are ordered and changeable and can contain duplicates. List can contain any number of items and of any type.

Lists are created using square brackets:

Creating a List

Let us define a list to store all the days in a week:

week = ["Sunday", "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday", "Saturday"]
print(week)

Output:

['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

Accessing List Items

The syntax for accessing an item is also pretty standard, simply provide the index of the item you want to access. Note that the first item has an index of 0:

week = ["Sunday", "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday", "Saturday"]

print(week[0])   # prints Sunday
print(week[1])   # prints Monday

You can use the negative index to access the items from the end of the list. For example.

week = ["Sunday", "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday", "Saturday"]
print(week[-1])    # prints Saturday

Changing Items

Lists defined in python are mutable. This means, we can update the values for a given index.

week = ["Sunday", "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday", "Saturday"]

week[1] = "MONDAY"
print(week) 

Output:

['Sunday', 'MONDAY', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

Appending Items to List

You can append item to list using the built in append() function. This method will add the new item to the end of the list.

animals = ["elephant", "tiger", "lion"]
print(animals)

animals.append("bear")
print(animals)

Output:

['elephant', 'tiger', 'lion']
['elephant', 'tiger', 'lion', 'bear']

Deleting Items from List

If you want to remove an item from the list, you can do that using the built in del function and passing in the index.

animals = ["elephant", "tiger", "lion", "bear"]
print(animals)

del(animals[1])
print(animals)

Output:

['elephant', 'tiger', 'lion', 'bear']
['elephant', 'lion', 'bear']

Alternatively, you can use the delete the item form list using the item value using the remove method:

animals = ["elephant", "tiger", "lion", "bear"]
print(animals)

animals.remove("tiger")
print(animals)

Output:

['elephant', 'tiger', 'lion', 'bear']
['elephant', 'lion', 'bear']

Looping through a List

Looping through a list is a very common operation and Python luckily makes this easy to do:

week = ["Sunday", "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday", "Saturday"]
for day in week:
	print(day)

Output:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Checking if an Item Exists in List

Sometimes you might be working with huge lists or not even know what is inside the list, in the case of lists being returned from methods.

You can very easily check if a list contains a value you want, like this:

week = ["Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"]

print("Monday" in week)

Output:

True

How to Check Length of a List?

You can check the length of the list by using the len() function:

week = ["Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"]

print(len(week))

Output:

7

How to Join Two Lists?

Python has a simple syntax for combining lists, use the + operator

na = ["USA", "Canada", "Mexico"]
eu = ["UK", "France", "Germany"]

countries = na + eu
print(countries)

Output:

['USA', 'Canada', 'Mexico', 'UK', 'France', 'Germany']

Reverse a List

Reverse the order of the items in a list using the reverse function:

week = ["Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"]

print(week)
week.reverse()
print(week)

Output:

['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
['Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday', 'Sunday']

Sort a list

You can easily sort a list using the built-in sort function:

week = ["Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"]

print(week)
week.sort()
print(week)

Output:

['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
['Friday', 'Monday', 'Saturday', 'Sunday', 'Thursday', 'Tuesday', 'Wednesday']

Getting Started with Java - Beginner's Guide

This course covers the fundamental aspects of core Java programming and provides a structured path for beginners to learn and practice Java programming.

>> CHECK OUT THE COURSE

Continue reading..