Learning Python is fun

Welcome to ‘learning python is fun - for beginners’

Previous: The Print Function

Next: Dictionaries

Python Lists, Tuples & Sets

Lesson: 7

Similar to strings, a List would hold a sequence of items or elements instead of single characters.
An item can be any data type: string, integer, float, etc.

>>> my_list1 = [0, 1.0, '3', []]
>>> print(my_list1)
[0, 1.0, '3', []]

List Declaration

we can declare a new list with square brackets [] or by calling the list function list()

click to run python

this will create a new empty list. we can also create a list with items inside it pretty easily

click to run python

Basic List Operations

Very similarly to strings, we can also perform addition and multiplication on lists

click to run python

Lists and strings are closely related, in fact we can convert one into the other

# create a list from a string
my_list7 = list("1234567")
print(my_list7)

# create a string from a list using .join
my_string1 = ''.join(my_list7)
print(my_string1)

# create a list from a string using .split
my_list8 = my_string1.split()
print(my_list8)

# get the size of the list
print(len(my_list8))

One major difference between a list and a string is that a list is mutable.
Meaning it can change; you can add items and remove items from a list.
This is not the case when working with strings, a string isimmutable.
When you perform changing operations on a string, you actually create a new string, the original value does not change. But a list can start off one way and end up completely different, here are some examples for you to play with:

# create a new list and assign it to a variable
my_list = []

print(my_list)
print('check the list size before it is mutated: ', len(my_list))

# use .append to add items to the list
my_list.append(1)
my_list.append(2)
my_list.append(3)

print(my_list)
print('check the list size again: ', len(my_list))

# use .append to add items to the list
my_list.pop()
my_list.pop(0)

print(my_list)
print(
  'list size after removing last and first items: ',
  len(my_list)
)

my_list.append(5)
print(my_list)

Calling .pop() with no arguments will pop (remove) the last item from the list. the pop function will also return the popped item in case we want to save it in a variable and use it later.

my_list = ['a', 'b', 'c']
item3 = my_list.pop()
item2 = my_list.pop()
item1 = my_list.pop()
print(my_list, item3, item2, item1)

When we want to remove an item from a specific position using .pop, we simply add a number as the first argument to the pop function.

my_list = ['a', 'b', 'c']
item1 = my_list.pop(0)
item3 = my_list.pop(1)
item2 = my_list.pop()
print(my_list, item1, item3, item2)

If you tried to run the code snippet above, you might have noticed the weird printing order of the items. This is because after we popped the item 'a' at position 0 (the first item on the list), the second item 'b' now became the first item on the list in position 0, and the last item 'c' is now in the second position 1 so when we popped the item at position 1 we removed 'c' instead of 'b'.

We can use .insert(position, item) to add an item to the list in a specific position.

my_list = ['a', 'b', 'c']
my_list.insert(1, 10)
my_list.insert(3, 10)
print(my_list)

It's best to always pop() and append(item) items to the end of the list for the best performance. You can read more about it here and here.

We can use the in operator to check if an item exists in a list

my_list = ['amazon', 'redhat', 'microsoft', 'google']
print('google' in my_list)

Similar to strings, getting a single item by its position index is pretty simple:

my_list = ['amazon', 'redhat', 'microsoft', 'google']
print(my_list[0])
print(my_list[1])
print(my_list[-1])
print(my_list[-2])

Lesson Completed 👏

Great job! 🍺🍺“🎉“🎉

In the next lesson we will learn all about Python Booleans & Conditional Programing

Previous: The Print Function

Next: Dictionaries