The Python posts on this page are based on what I learned from the book Jump to Python, interpreted and organized in my own way. If you find any mistakes or inaccuracies, please feel free to let me know, I’ll review and correct them as soon as possible.
A list in Python is a data structure that stores multiple values in a specific order.
Unlike strings (which can only store sequences of characters), lists can store different data types together, numbers, strings, booleans, or even other lists.
students = ["Tom", "Jane", "Chris"]
numbers = [10, 20, 30, 40, 50]
mixed = ["apple", 12, True, 3.14]
Python
복사
Lists are created using square brackets [], and elements are separated by commas.
Like strings, lists support indexing and slicing.
How to Create and Use Lists
There are two main ways to create a list in Python.
(1) Using Square Brackets []
This is the most common and straightforward method. Simply write the elements inside square brackets, separated by commas.
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hi", 10, True]
Python
복사
This approach is used when you’re defining a new list directly.
In most real-world code, lists are created using this literal syntax.
(2) Using the list() Function
The list() function is mainly used when you want to convert another data type into a list.
It works with iterable objects such as strings, tuples, or range objects.
text = "hello"
chars = list(text)
print(chars) # ['h', 'e', 'l', 'l', 'o']
nums = list(range(5))
print(nums) # [0, 1, 2, 3, 4]
t = (1, 2, 3)
lst = list(t)
print(lst) # [1, 2, 3]
Python
복사
list() is not just a way to “create a list,” but rather a way to
convert other iterable data types into list form.
So generally
•
Use [] → when creating a new list
•
Use list() → when converting another iterable (like a string or tuple) into a list
Indexing and Slicing Lists
Each element in a list has an index starting from 0. You can access any element using its index. Negative indices count from the end.
colors = ["red", "green", "blue"]
print(colors[0]) # red
print(colors[-1]) # blue
Python
복사
Slicing
Slicing extracts a portion of a list. Use [start:end] — note that the end index is exclusive.
nums = [1, 2, 3, 4, 5, 6]
print(nums[1:4]) # [2, 3, 4]
print(nums[:3]) # [1, 2, 3]
print(nums[3:]) # [4, 5, 6]
Python
복사
You can also include a step value [start:end:step].
print(nums[::2]) # [1, 3, 5]
print(nums[::-1]) # [6, 5, 4, 3, 2, 1] (reverse order)
Python
복사
List Operations
Lists support arithmetic-like operations such as addition and multiplication.
(1) Concatenation (+)
You can combine two lists into one.
a = [1, 2, 3]
b = [4, 5]
print(a + b) # [1, 2, 3, 4, 5]
Python
복사
This creates a new list, the original lists remain unchanged.
(2) Repetition (*)
Repeats a list multiple times.
a = ["Hi"]
print(a * 3) # ['Hi', 'Hi', 'Hi']
Python
복사
Similar to string repetition, but repeats the entire list.
(3) Length
Use the len() function to count the total number of elements.
a = [1, 2, 3, 4]
print(len(a)) # 4
Python
복사
Modifying and Deleting List Elements
Lists are mutable, meaning their contents can be changed after creation.
(1) Modifying Values
You can reassign an element at a specific index.
a = [10, 20, 30]
a[1] = 99
print(a) # [10, 99, 30]
Python
복사
(2) Deleting Elements
Use the del keyword to remove elements by index.
a = [1, 2, 3, 4]
del a[2]
print(a) # [1, 2, 4]
Python
복사
You can also delete the entire list using del a.
List Methods
Python lists come with many built-in methods for data manipulation.
(1) append() — Add an Element
Adds a new element to the end of the list.
a = [1, 2]
a.append(3)
print(a) # [1, 2, 3]
Python
복사
(2) sort() — Sort the List
Sorts the list in ascending order by default.
a = [3, 1, 2]
a.sort()
print(a) # [1, 2, 3]
Python
복사
For descending order, use reverse=True.
a.sort(reverse=True)
print(a) # [3, 2, 1]
Python
복사
(3) reverse() — Reverse the Order
Simply reverses the current order of elements.
a = [1, 2, 3]
a.reverse()
print(a) # [3, 2, 1]
Python
복사
(4) index() — Find the Index of a Value
Returns the position of the first matching value.
a = ["a", "b", "c", "d"]
print(a.index("c")) # 2
Python
복사
If the value doesn’t exist, Python raises an error.
(5) insert() — Insert a Value
Inserts a new element at a specific position.
a = [1, 2, 4]
a.insert(2, 3)
print(a) # [1, 2, 3, 4]
Python
복사
(6) remove() — Remove a Value
Removes the first matching value from the list.
a = [1, 2, 3, 2]
a.remove(2)
print(a) # [1, 3, 2]
Python
복사
(7) pop() — Extract and Return an Element
Removes and returns an element by index. If no index is given, removes the last element.
a = [10, 20, 30]
value = a.pop()
print(value) # 30
print(a) # [10, 20]
Python
복사
(8) count() — Count Occurrences
Counts how many times a specific value appears.
a = [1, 2, 2, 3, 2]
print(a.count(2)) # 3
Python
복사
(9) extend() — Merge Two Lists
Adds all elements from another list to the current one. Unlike append(), it doesn’t nest the list, it expands it.
a = [1, 2]
b = [3, 4]
a.extend(b)
print(a) # [1, 2, 3, 4]
Python
복사



