Back to Blogs
What Is a List in Python?

What Is a List in Python?

Murat Hüdavendigâr Öncü
5 min
#python#programming#data-structures#coding-basics#backend

A comprehensive beginner's guide to Python lists, covering creation, indexing, slicing, and essential list methods with practical code examples.

If you are starting your journey in Python, one of the very first data structures you will encounter is the list. A list in Python is exactly what it sounds like: a container that holds a collection of items in a specific order.

Unlike variables that can only store a single value, lists allow you to group multiple values together under a single name. This makes managing, organizing, and manipulating data significantly easier.

Understanding the Basics

In Python, lists are incredibly flexible. They are 'mutable,' meaning you can change their content after they are created. Furthermore, a single list can hold items of different data types, such as integers, strings, and even other lists.

You create a list by placing elements inside square brackets [], separated by commas.

# A list of strings
fruits = ["apple", "banana", "cherry"]

# A list of mixed data types
mixed_list = [42, "Hello World", 3.14, True]

# An empty list
empty_list = []

Accessing Elements via Indexing

Python lists are ordered, which means every item has a specific position, known as its index. Python uses zero-based indexing, meaning the first item is at index 0, the second is at index 1, and so on.

Python also supports negative indexing, which is a highly convenient feature. An index of -1 refers to the last item, -2 refers to the second to last, and so forth.

colors = ["red", "green", "blue", "yellow"]

print(colors[0])   # Output: red
print(colors[2])   # Output: blue
print(colors[-1])  # Output: yellow
print(colors[-3])  # Output: green

Modifying a List

Because lists are mutable, you can easily update individual elements by targeting their index and assigning a new value.

programming_languages = ["Python", "Java", "C++"]
programming_languages[1] = "JavaScript"

print(programming_languages) 
# Output: ['Python', 'JavaScript', 'C++']

Adding Items to a List

Python provides several built-in methods to expand your lists dynamically.

  1. append(): Adds a single item to the very end of the list.
  2. insert(): Adds an item at a specific index, shifting the other elements to the right.
  3. extend(): Appends elements from another list (or any iterable) to the end of the current list.
numbers = [1, 2, 3]

# Using append
numbers.append(4)
print(numbers)  # Output: [1, 2, 3, 4]

# Using insert (insert '100' at index 1)
numbers.insert(1, 100)
print(numbers)  # Output: [1, 100, 2, 3, 4]

# Using extend
more_numbers = [5, 6]
numbers.extend(more_numbers)
print(numbers)  # Output: [1, 100, 2, 3, 4, 5, 6]

Removing Items from a List

Just as you can add items, you can also remove them using various methods depending on your needs.

  1. remove(): Removes the first occurrence of a specific value.
  2. pop(): Removes and returns the item at a specific index. If no index is provided, it removes and returns the last item.
  3. clear(): Empties the entire list.
animals = ["cat", "dog", "rabbit", "dog"]

# Remove specific value
animals.remove("dog")
print(animals)  # Output: ['cat', 'rabbit', 'dog'] (Notice only the first 'dog' is removed)

# Pop the last item
last_animal = animals.pop()
print(last_animal)  # Output: dog
print(animals)      # Output: ['cat', 'rabbit']

Slicing a List

Slicing is a powerful feature that allows you to extract a portion of a list to create a new, smaller list. The syntax is list[start:stop:step].

  • start: The index where the slice begins (inclusive).
  • stop: The index where the slice ends (exclusive).
  • step: The interval between elements.
alphabet = ["A", "B", "C", "D", "E", "F", "G"]

# Slice from index 1 to 4
print(alphabet[1:4])  # Output: ['B', 'C', 'D']

# Slice from the beginning to index 3
print(alphabet[:3])   # Output: ['A', 'B', 'C']

# Slice from index 4 to the end
print(alphabet[4:])   # Output: ['E', 'F', 'G']

# Get every second element
print(alphabet[::2])  # Output: ['A', 'C', 'E', 'G']

Conclusion

Lists are the backbone of data manipulation in Python. They are intuitive, highly adaptable, and come packed with built-in methods that save you from writing complex loops for simple tasks. By mastering list creation, indexing, slicing, and methods, you establish a strong foundation for handling complex algorithms and data structures in your future Python projects.


Read the original full version on Medium

Comments (0)