DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Python Tutorial - 3 Data Structure

Introduction

The data structure is a tool for organizing data. Not only for storage but also for solving some problems. There are some data structures in Python including list, dictionary, tuple, and set.

List

A list is a data structure that stores an item sequentially using indices. This is the illustration of the list data structure.

List in Python Illustration

There are many ways to create a list in Python.

  1. Initialize the value directly inside a list.
items = [1,2,3,4]
Enter fullscreen mode Exit fullscreen mode
  1. Initialize an empty list.
items = []
Enter fullscreen mode Exit fullscreen mode

The item inside the list can be accessed directly via the index.

items = [1,2,3,4,5]

# access item at index 2
result = items[2]

print(result) # returns 3
Enter fullscreen mode Exit fullscreen mode

All items inside the list can be retrieved using the for loop. This is an example.

# create a new list
items = [1,2,3,4,5]

# retrieve each item inside a list
for item in items:
    print(item)
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Based on the code above, the list called items is created with assigned items. Each item is retrieved using the for loop.

List Basic Operations

The append() function adds a new item to a list. This is an example of append() usage.

# create empty list
shopping_list = []

# add some items
shopping_list.append("apple")
shopping_list.append("milk")
shopping_list.append("cereal")

# retrieve all items
for item in shopping_list:
    print(item)
Enter fullscreen mode Exit fullscreen mode

Output

apple
milk
cereal
Enter fullscreen mode Exit fullscreen mode

The append() is illustrated in the picture below.

append illustration

Besides the append() function, the insert() function adds a new item in a specific index. This is an example.

items = ["apple","banana","mango","coffee"]

# add new item at index 1
items.insert(1,"cereal")

# retrieve all items
for item in items:
    print(item)
Enter fullscreen mode Exit fullscreen mode

Output

apple
cereal
banana
mango
coffee
Enter fullscreen mode Exit fullscreen mode

The insert() is illustrated in the picture below.

insert illustration

Updating an item inside a list is straightforward. Just specify the index of the item then change it with the updated item.

# create a list
drinks = ["milkshake","black tea","banana milk","mango juice"]

# update value at index 2
drinks[2] = "chocolate milk"

print(f"value at index 2: {drinks[2]}")
Enter fullscreen mode Exit fullscreen mode

Output

value at index 2: chocolate milk
Enter fullscreen mode Exit fullscreen mode

The remove() function removes an item from the list. This is an example.

items = ["apple","banana","mango","coffee"]

# remove "mango"
items.remove("mango")

# remove item at index 1
items.remove(items[1])

print("after removed")
for item in items:
    print(item)
Enter fullscreen mode Exit fullscreen mode

Output

after removed
apple
coffee
Enter fullscreen mode Exit fullscreen mode

The items inside a list can be selected by specifying the start index and end index of the list. This is the basic structure for selecting items in a list.

list_name[start:end]
Enter fullscreen mode Exit fullscreen mode