3 Python Lists Flashcards
(23 cards)
What is a list?
A list is a collection of ordered, changeable items.
words = [‘NLP’, ‘is’, ‘fun’]
How do I add a new word to the end of my tokens list?
append()
tokens = [‘hello’, ‘world’]
tokens.append(‘NLP’)
# [‘hello’, ‘world’, ‘NLP’]
How do I add all words from another list?
extend()
tokens = [‘hello’]
tokens.extend([‘world’, ‘NLP’])
# [‘hello’, ‘world’, ‘NLP’]
How do I insert a word at a specific position?
insert()
tokens = [‘hello’, ‘NLP’]
tokens.insert(1, ‘world’)
# [‘hello’, ‘world’, ‘NLP’]
How do I remove and get the last word?
pop()
tokens = [‘hello’, ‘world’]
word = tokens.pop()
# ‘world’, and tokens → [‘hello’]
How do I delete a specific word?
remove()
tokens = [‘hello’, ‘world’, ‘hello’]
tokens.remove(‘hello’)
# [‘world’, ‘hello’] (removes first occurrence)
Where is the word ‘world’ in the list?
index()
tokens = [‘hello’, ‘world’]
tokens.index(‘world’) # 1
How many times does ‘hello’ appear?
count()
tokens = [‘hello’, ‘hello’, ‘world’]
tokens.count(‘hello’) # 2
how do I sort my list alphabetically?
sort()
tokens = [‘banana’, ‘apple’, ‘cherry’]
tokens.sort()
# [‘apple’, ‘banana’, ‘cherry’]
How to reverse a list order?
reverse()
tokens = [‘a’, ‘b’, ‘c’]
tokens.reverse()
# [‘c’, ‘b’, ‘a’]
How do I make a copy before editing?
copy()
tokens = [‘NLP’, ‘ML’]
backup = tokens.copy()
How to delete all elements?
clear()
tokens = [‘delete’, ‘everything’]
tokens.clear()
# []
del keyword in list uses(del (keyword, not a method) )
tokens = [‘a’, ‘b’, ‘c’]
del tokens[1]
# [‘a’, ‘c’]
List functions and keywords summary
Method Description
append(x)
Adds element x to the end of the list.
extend(iter)
Adds all elements from another iterable (e.g. list, set) to the end.
insert(i, x)
Inserts element x at position i.
pop([i])
Removes and returns element at index i (default: last).
remove(x)
Removes the first occurrence of element x.
index(x)
Returns the index of the first occurrence of x.
count(x)
Returns the number of times x appears.
sort()
Sorts the list in place (alphabetically or numerically).
reverse()
Reverses the list in place.
copy()
Returns a shallow copy of the list.
clear()
Removes all elements from the list.
del list[i]
Deletes item at index i using del.
sorted(list)
Returns a new sorted list, leaving the original unchanged.
what is List comprehension?
List comprehension is a shorter, cleaner way to create a new list by applying an expression to each item in an existing iterable (like a list, string, or range).
Instead of writing a loop, you do it all in one line.
List Comprehension syntax
[expression for item in iterable if condition]
expression
What you want to do with each item (e.g., x+1, x.lower(), len(x))
item
Each element from the iterable (like x in a loop)
iterable
Any collection (like list, range, string, etc.)
if condition (optional)
Filter (only include items that satisfy this)
Normal code
for word in words:
print(word.upper())
Make a new list by adding 1 to every number in an old list.
nums = [1, 2, 3]
new_nums = [x + 1 for x in nums]
# Output: [2, 3, 4]
Convert all words in the list to lowercase
words = [‘NLP’, ‘Python’, ‘FUN’]
cleaned = [w.lower() for w in words]
# Output: [‘nlp’, ‘python’, ‘fun’]
Get lengths of words in the list
words = [‘hi’, ‘hello’, ‘bye’]
lengths = [len(w) for w in words]
# Output: [2, 5, 3]
Square each number in the list
nums = [2, 3, 4]
squares = [x ** 2 for x in nums]
# Output: [4, 9, 16]
Explain Slicing
Slicing is not limited to just lists — it’s a feature of sequences in Python.
Slicing works on:
Data Type Example
list my_list[1:4]
string ‘hello’[1:4] → ‘ell’
tuple (1, 2, 3, 4)[1:3] → (2, 3)
range (converted to list)
list(range(10))[2:5] → [2, 3, 4]
Slicing Syntax
sequence[start:stop:step]
start → starting index (inclusive)
stop → ending index (exclusive)
step → how many steps to jump (optional)
Slicing Examples
Simple forward slicing
nums = [10, 20, 30, 40]
print(nums[1:3]) # [20, 30]
Reverse a string
word = “NLP”
print(word[::-1]) # “PLN”
understand more on step