A Beginner's Guide to Python Lists

A Beginner's Guide to Python Lists


python

Do you have a collection of data that doesn’t require random access and needs frequent modifications? Look no further! Python lists are here to make your life easier. Let’s dive into the world of lists and explore their magic together.

What is a List?

A list is a versatile data structure in Python that can hold multiple values, often referred to as items. Embracing simplicity, lists are perfect for creating iterable collections that undergo frequent modifications.

Here’s a quick glimpse of how a list in Python looks like:

days = ["Monday", "Tuesday", "Wednesday", "Thursday"]

Notice the square brackets? That’s the list’s signature look!

Now, let’s talk indexes. Each item in a list is assigned an integer index, starting from 0. For instance:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
first_day = days[0]
print(first_day)    # 'Monday'

Negative indexes work too! -1 refers to the last item, -2 to the second last, and so forth.

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
last_day = days[-1]
print(last_day)     # 'Friday'

Lists within Lists

Lists can also contain other lists, creating a dynamic structure:

days = [["Monday", "Tuesday"], ["Wednesday", "Thursday", "Friday"]]

To access items in these nested lists, use multiple indexes:

days[0]     # ['Monday', 'Tuesday']
days[0][1]  # 'Tuesday'

Slicing and Dicing

Get ready for some slicing action! Extract multiple items with a slice:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
get_tues_wed = days[1:3]
print(get_tues_wed)     # ['Tuesday', 'Wednesday']

Remember, the slice starts at the first index and goes up to (but doesn’t include) the second index.

Modifying Lists

Modify single items using indexes:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
days[1] = "Sunday"
print(days)     # ["Monday", "Sunday", "Wednesday", "Thursday"]

You can also replace items using slices:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
days[1:3] = ['a', 'b', 'c']
print(days)     # ['Monday', 'a', 'b', 'c', 'Thursday', 'Friday]

Handy Shortcuts

Slices have shortcuts too:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
first_two_days = days[:2]
print(first_two_days)   # ['Monday', 'Tuesday']

The provided code outputs the days starting from the first index up to, but not including, the third index.

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
third_to_last_day = days[2:]
print(third_to_last_day)    # ['Wednesday', 'Thursday', 'Friday']

The above code prints the days from the third to the last index.

Deleting from a List

Use the del statement to bid farewell to an item:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
del days[2]
print(days)     # ['Monday', 'Tuesday', 'Thursday', 'Friday']

List Operations

Just like strings, you can use len() to find the number of items in a list:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
no_of_days = len(days)
print(no_of_days)   # 5

Concatenate lists for more fun:

my_concat = ['a', 'b', 'c'] + ['d', 'e', 'f']
print(my_concat)    # ['a', 'b', 'c', 'd', 'e', 'f']

Replication? Absolutely!

my_repl = ['a', 'b'] * 3
print(my_repl)   # ['a', 'b', 'a', 'b', 'a', 'b']

Converting Values

Transform values into lists with the list() function:

convert_to_list = list('Hello')
print(convert_to_list)    # ['H', 'e', 'l', 'l', 'o']

Checking for Values

Use the in and not in operators to check for values:

my_list = ['learning', 'python', 'is', 'fun', 'yeah']
print('yeah' in my_list)    # True
my_list = ['learning', 'python', 'is', 'fun', 'yeah']
print('yeah' not in my_list)    # False

In essence, working with lists in Python is a breeze. Embrace the power of lists, and let your data dance to your tune! Happy coding! ✨

© 2024 Tettei Shahday-Annang