Python: List [Data Type]

List in Python is a data type that stores a collection of data, of one or more types. Python list is mutable.

Lists are ordered collections of items. Items in the list store the order of each item.

Define a List

Define a list-

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

print("Type: ", type(big_box_list))
print("Length: ", len(big_box_list))

print(dir(big_box_list))
Python

Output:

Type:  <class 'list'>
Length:  7

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Plaintext

Or we can define a list like below-

big_box_list = list()

print("Type: ", type(big_box_list))
print("Length: ", len(big_box_list))

print(dir(big_box_list))

# Add items using append()
# big_box_list.append("one")
# big_box_list.append("two")
# big_box_list.append("three")
Python

Output:

Type:  <class 'list'>
Length:  0

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Plaintext

Item Index in List

Index in a list starts from zero(0). And item at a certain index can be accessed by using “list_name[index]”.

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

print(big_box_list[0])
print(big_box_list[4])
print(big_box_list[6])
print(big_box_list[-1])
Python

Output:

First item
5
Last Item here
Last Item here
Plaintext

Change List Item

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

big_box_list[0] = 11
big_box_list[3] = "Changed"

print(big_box_list)
Python

Output:

[11, 'Second item', 'Third item', 'Changed', 5, 100, 'Last Item here']
Plaintext

Add Item to List

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]


big_box_list.append("New item here")
big_box_list.append("Second new item")

print(big_box_list)
Python

Output:

['First item', 'Second item', 'Third item', 4, 5, 100, 'Last Item here', 'New item here', 'Second new item']
Plaintext

Check Length of List

Use the “len()” function to get the length of a list-

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]


list_length = len(big_box_list)

print(list_length)
Python

Output:

7
Plaintext

Concat Lists

We can concat 2 or more lists, using the “+” operator-

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

second_list = [99, 999, 99.99, 9090]

print(big_box_list + second_list)
Python

Output:

['First item', 'Second item', 'Third item', 4, 5, 100, 'Last Item here', 99, 999, 99.99, 9090]
Plaintext

Slicing a List

We can get a certain portion of a list by slicing it-

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

print(big_box_list[0:3])
print(big_box_list[:3])
print(big_box_list[2:7])
print(big_box_list[:-1])
Python

Output:

['First item', 'Second item', 'Third item']
['First item', 'Second item', 'Third item']
['Third item', 4, 5, 100, 'Last Item here']
['First item', 'Second item', 'Third item', 4, 5, 100]
Plaintext

Check Length of List

Use the “len()” function to get the length of a list-

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]


list_length = len(big_box_list)

print(list_length)
Python

Output:

7
Plaintext

Loop through a List

Case #1:

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

for item in big_box_list:
    print(item)
Python

Output:

First item
Second item
Third item
4
5
100
Last Item here
Plaintext

Case #2: Using while Loop

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]
    
# Start from index 0
item_index = 0

while item_index < len(big_box_list):
    print(f"Index: {item_index} || Item value: {big_box_list[item_index]}")
    
    # Increase the index by 1
    item_index += 1
Python

Output:

Index: 0 || Item value: First item
Index: 1 || Item value: Second item
Index: 2 || Item value: Third item
Index: 3 || Item value: 4
Index: 4 || Item value: 5
Index: 5 || Item value: 100
Index: 6 || Item value: Last Item here
Plaintext

Case #3: Using enumerate With for Loop

big_box_list = ["First item", "Second item", "Third item", 4, 5, 100, "Last Item here"]

for key, value in enumerate(big_box_list):
    print(f"Key: {key} || Value: {value}")
Python

Output:

Key: 0 || Value: First item
Key: 1 || Value: Second item
Key: 2 || Value: Third item
Key: 3 || Value: 4
Key: 4 || Value: 5
Key: 5 || Value: 100
Key: 6 || Value: Last Item here
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.