Data Structures - List

Lists are used to store multiple items in a single variable. They are collections that hold items (similar to array). Lists are ordered and changeable.

  • Mutable - the programmer can edit the list after its creation - removing or adding items.
  • Ordered - the indexes of the items don't change and remain the same.

A list can be created by placing the items inside square brackets [item1, item2]. The items are separated by a comma. Here is an example:

#Creating a list my_list = [1, 2, "python", 3, "tutorial", True] print(my_list)

Some operators can operate with Lists!
Python concatenates Lists using the + operator.
It is possible to multiply Lists using the * operator.

#Operators and Lists a_list = ["python", "tutorial"] b_list = [5, 10, 20] print(a_list + b_list) print(a_list * 5)

Access Items

It is possible to access to different items of a list by putting the appropriate index in square brackets next to the list - list[index]. The index of the first item is 0. It is possible to use the negative index. The negative index of the last item is -1.

Slicing - access to a specified range of elements. Slicing is made by adding square brackets next to the list with the appropriate start index and the end index separated by colon.
The syntax:

  • list[stop]
  • list[start : stop]
  • list[start : stop : step]

#Access Items my_list = [1, 2, "python", 3, "tutorial", True, "developer"] print(my_list) print("The first item of the list is ", my_list[0]) print("The last item of the list is ", my_list[-1]) #Slicing print(my_list[0:-1:2]) print(my_list[2:4]) print(my_list[1:-2])

List - Functions and Methods

Len

The len() function returns the length of a list.

#len() our_list = ["blue", "green", "gray", "orange"] print(len(our_list)) print(len(our_list) * 2)

Append

The append() method is used to add items to the list.

#append our_list = ["blue", "green", "gray", "orange"] print(our_list) our_list.append("yellow") print(our_list) our_list.append("pink") print(our_list)

Remove

The remove() method is used to remove items from the list. The remove method removes the first occurrence of value. If the value is not present it raises an error!

#remove our_list = ["blue", "green", "gray", "orange", "blue", "green", "gray", "orange"] print(our_list) our_list.remove("orange") print(our_list) our_list.remove("orange") print(our_list) our_list.remove("orange") print(our_list)

In the example above the original list has 2 "orange" items. The first time we use the remove method the first occurrence of the item is removed. The seconde time, the second occurrence of the item is removed. After that there is no longer any "orange" item in our_list - error.

Count

The count() method retuns the number of occurrences of a value.

#count our_list = ["tree", "tree", "leaf", "leaf", "leaf"] print(our_list.count("tree")) print(our_list.count("leaf"))

Exercise 1

Try to create a list and then slice it

#Slicing #Create your list here: #Slice it as you wish :)

Exercise 2

  1. Create an empty list.
  2. Add as many items as you wish.
  3. Remove the first item you added.
  4. Print the list.
  5. Print its first item.

#Create an empty list #Add items #Remove an item #Print list #Print item

Exercise 3

  1. Count the number of occurrences of the string "python"
  2. Remove the all the items that are not "python"

my_list = ["python", "python", "python", "python", "python", "tutorial"] #1. Count the number of occurrences of the string "python" #2. Remove an item