Lists and Loops in Python

Lists in Python

What Are Lists?

  • A list is an ordered collection of items.
  • Items can be any data type: numbers, strings, or other lists.
  • Lists are mutable, meaning you can modify them after creation.
fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']

Why Use Lists in NLP?

  • Lists can store words, sentences, or entire texts.
  • NLP tasks often require managing large datasets of tokens or documents.
sentence = "Natural language processing is fun"
tokens = sentence.split()  # Tokenize sentence into words
print(tokens)
['Natural', 'language', 'processing', 'is', 'fun']

Accessing List Elements

Indexing

  • Access list elements using indexing.
  • Python indexing starts at 0.
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Outputs: apple
print(fruits[1])  # Outputs: banana
apple
banana

Negative Indexing

  • Negative indexing starts from the end of the list.
print(fruits[-1])  # Outputs: cherry (last element)
cherry
  • Useful for quickly accessing the last few elements of a list.

Modifying Lists

Changing Elements

  • Modify a list by assigning a new value at a specific index.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"  # Replacing 'banana'
print(fruits)
['apple', 'blueberry', 'cherry']

Real-World NLP Use

  • You might replace tokens or correct errors in a tokenized sentence.
tokens = ["NLP", "is", "coool"]
tokens[2] = "cool"  # Fix typo
print(tokens)
['NLP', 'is', 'cool']

Adding and Removing Elements

Adding Elements

  • Use .append() to add items to the end of the list.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
['apple', 'banana', 'cherry']

Removing Elements

  • Use .remove() to delete specific items.
fruits.remove("banana")
print(fruits)
['apple', 'cherry']
  • Lists are flexible, allowing easy manipulation in NLP tasks.

List Operations: Length, Sorting, and Checking

List Length

  • Use len() to find the number of elements in a list.
fruits = ["banana", "cherry", "apple"]
print(len(fruits))
3

Sorting and Checking Elements

  • Use .sort() to sort the list.
  • Use in to check if an item exists in a list.
fruits.sort()
print(fruits)

print("apple" in fruits)
print("grape" in fruits)
['apple', 'banana', 'cherry']
True
False

Loops in Python

What Are Loops?

  • Loops help you iterate over a list and perform actions on each element.
  • Commonly used in NLP for tasks like text processing.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
apple
banana
cherry

Real-World NLP Example

  • Tokenizing and processing text using loops.
tokens = ["NLP", "is", "fun"]
for token in tokens:
    print(token)
NLP
is
fun

Using Loops for Simple Operations

Squaring Numbers in a List

  • Loops allow you to apply operations to list elements.
numbers = [1, 2, 3, 4]
squares = []
for number in numbers:
    squares.append(number ** 2)

print(squares)
[1, 4, 9, 16]

NLP Example: Word Length Calculation

  • Use loops to calculate the length of each word in a sentence.
tokens = ["NLP", "is", "great"]
for token in tokens:
    print(f"{token}: {len(token)} characters")
NLP: 3 characters
is: 2 characters
great: 5 characters

Looping with range()

Using range()

  • Sometimes you need to iterate a set number of times.
  • range() generates a sequence of numbers.
for i in range(5):
    print(i)
0
1
2
3
4

Combining range() with Lists

  • Use range() to loop over list indices, allowing more control.
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")
Index 0: apple
Index 1: banana
Index 2: cherry

Common List Methods

Adding and Removing Elements

  • .append(): Adds an item to the end.
  • .remove(): Removes a specific item.
fruits.append("orange")
fruits.remove("banana")
print(fruits)
['apple', 'cherry', 'orange']

Inserting and Popping Elements

  • .insert(): Inserts an item at a specific index.
  • .pop(): Removes and returns the last item or the item at a specific index.
fruits.insert(1, "blueberry")
print(fruits)

last_fruit = fruits.pop()
print(fruits)
['apple', 'blueberry', 'cherry', 'orange']
['apple', 'blueberry', 'cherry']

Summary: Lists and Loops

  • Lists: Allow you to store and manipulate multiple elements, useful in NLP for text processing.
  • Loops: Help automate repetitive tasks and process list elements.
  • You can easily modify, sort, and check elements within lists.
  • Loops and lists combined allow for efficient handling of data in Python, especially in NLP.