3 Python Lists Flashcards

(23 cards)

1
Q

What is a list?

A

A list is a collection of ordered, changeable items.
words = [‘NLP’, ‘is’, ‘fun’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do I add a new word to the end of my tokens list?

A

append()

tokens = [‘hello’, ‘world’]
tokens.append(‘NLP’)
# [‘hello’, ‘world’, ‘NLP’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do I add all words from another list?

A

extend()

tokens = [‘hello’]
tokens.extend([‘world’, ‘NLP’])
# [‘hello’, ‘world’, ‘NLP’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do I insert a word at a specific position?

A

insert()

tokens = [‘hello’, ‘NLP’]
tokens.insert(1, ‘world’)
# [‘hello’, ‘world’, ‘NLP’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do I remove and get the last word?

A

pop()

tokens = [‘hello’, ‘world’]
word = tokens.pop()
# ‘world’, and tokens → [‘hello’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do I delete a specific word?

A

remove()

tokens = [‘hello’, ‘world’, ‘hello’]
tokens.remove(‘hello’)
# [‘world’, ‘hello’] (removes first occurrence)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Where is the word ‘world’ in the list?

A

index()

tokens = [‘hello’, ‘world’]
tokens.index(‘world’) # 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many times does ‘hello’ appear?

A

count()

tokens = [‘hello’, ‘hello’, ‘world’]
tokens.count(‘hello’) # 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do I sort my list alphabetically?

A

sort()

tokens = [‘banana’, ‘apple’, ‘cherry’]
tokens.sort()
# [‘apple’, ‘banana’, ‘cherry’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to reverse a list order?

A

reverse()

tokens = [‘a’, ‘b’, ‘c’]
tokens.reverse()
# [‘c’, ‘b’, ‘a’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do I make a copy before editing?

A

copy()

tokens = [‘NLP’, ‘ML’]
backup = tokens.copy()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to delete all elements?

A

clear()

tokens = [‘delete’, ‘everything’]
tokens.clear()
# []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

del keyword in list uses(del (keyword, not a method) )

A

tokens = [‘a’, ‘b’, ‘c’]
del tokens[1]
# [‘a’, ‘c’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List functions and keywords summary

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is List comprehension?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List Comprehension syntax

A

[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())

17
Q

Make a new list by adding 1 to every number in an old list.

A

nums = [1, 2, 3]
new_nums = [x + 1 for x in nums]
# Output: [2, 3, 4]

18
Q

Convert all words in the list to lowercase

A

words = [‘NLP’, ‘Python’, ‘FUN’]
cleaned = [w.lower() for w in words]
# Output: [‘nlp’, ‘python’, ‘fun’]

19
Q

Get lengths of words in the list

A

words = [‘hi’, ‘hello’, ‘bye’]
lengths = [len(w) for w in words]
# Output: [2, 5, 3]

20
Q

Square each number in the list

A

nums = [2, 3, 4]
squares = [x ** 2 for x in nums]
# Output: [4, 9, 16]

21
Q

Explain Slicing

A

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]

22
Q

Slicing Syntax

A

sequence[start:stop:step]

start → starting index (inclusive)

stop → ending index (exclusive)

step → how many steps to jump (optional)

23
Q

Slicing Examples

A

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