Confusing Operations in Python
Learn the differences between Python operations and operators that are often confused by beginners and frequently asked in interviews.
1. append() vs extend()
append() and extend() are methods of the Python list type. Both are used to add elements to a list, but they behave differently.
| Method | Available On | Purpose |
|---|---|---|
append() |
list only |
Adds a single object to the end of the list. |
extend() |
list only |
Adds each element from an iterable to the end of the list. |
append()
append() adds one object to the end of the list.
Output
If the object is another list, the entire list is added as a single element.
Output
extend()
extend() expects an iterable and adds each of its elements individually.
Output
Since strings are iterable, extend() adds one character at a time.
Output
Summary
append() |
extend() |
|---|---|
| Adds a single object | Adds multiple elements |
| Accepts any object | Expects an iterable |
| Nested lists remain nested | Elements are added individually |
Available only on list |
Available only on list |
2. append() vs insert()
append() and insert() are methods of the Python list type. Both add elements to a list, but they differ in where the new element is placed.
| Method | Available On | Purpose |
|---|---|---|
append() |
list only |
Adds an element to the end of the list. |
insert() |
list only |
Inserts an element at a specified index. |
append()
append() always adds the new element at the end of the list.
Output
insert()
insert(index, object) inserts the object before the specified index.
Output
If the index is greater than the list length, the element is added at the end.
Output
3. remove() vs pop() vs del
remove() and pop() are methods of the Python list type, whereas del is a Python statement that can remove items from many types or even delete entire variables.
| Operation | Available On | Purpose |
|---|---|---|
remove() |
list only |
Removes the first occurrence of a specified value. |
pop() |
list only |
Removes and returns an element by index. |
del |
Lists, dictionaries, variables, and more | Deletes items, slices, or entire objects. |
remove()
remove(value) removes the first matching value from the list.
Output
pop()
pop(index) removes and returns the element at the given index.
Output
If no index is given, the last element is removed.
Output
del
del can remove an item by index, delete a slice, or delete an entire variable.
Delete an item:
Output
Delete a slice:
Output
Delete a variable:
4. sort() vs sorted()
sort() and sorted() are both used to sort data, but they differ in where they are available and whether they modify the original object.
| Operation | Available On | Purpose |
|---|---|---|
sort() |
list only |
Sorts the list in place. |
sorted() |
Any iterable | Returns a new sorted list without modifying the original object. |
sort()
sort() modifies the original list.
Output
sorted()
sorted() returns a new sorted list, leaving the original object unchanged.
Output
sorted() also works with other iterables such as tuples, sets, and strings.
Output
Summary
sort() |
sorted() |
|---|---|
| List method | Built-in function |
Available only on list |
Works with any iterable |
| Modifies the original list | Returns a new sorted list |
Returns None |
Returns the sorted list |
What type of functions are they?
| Operation | Type | Available On |
|---|---|---|
sort() |
Instance method | list objects only |
sorted() |
Built-in function | Any iterable |
Summary
remove() |
pop() |
del |
|---|---|---|
| Removes by value | Removes by index | Deletes by index, slice, or entire object |
| Returns nothing | Returns the removed element | Returns nothing |
| List method | List method | Python statement |
Available only on list |
Available only on list |
Works with many object types |
Summary
append() |
insert() |
|---|---|
| Adds an element at the end | Adds an element at a specified position |
Takes one argument: object |
Takes two arguments: index, object |
| Faster for adding to the end | Existing elements may be shifted |
Available only on list |
Available only on list |
5. is vs ==
is and == are both used to compare objects, but they compare different things.
==checks whether two objects have the same value.ischecks whether two variables refer to the same object in memory.
| Operator | Purpose |
|---|---|
== |
Compares the values of two objects. |
is |
Compares the identity (memory reference) of two objects. |
==
Use == when you want to check whether two objects contain the same value.
Output
Although list1 and list2 are different objects, they contain the same values.
is
Use is when you want to check whether two variables refer to the same object.
Output
Both variables refer to the same list object.
If two variables refer to different objects, is returns False, even if their values are equal.
Output
When should you use is?
The most common use of is is to compare an object with None.
Using is None is the recommended Python style.
Summary
== |
is |
|---|---|
| Compares values | Compares object identity |
| Checks if values are equal | Checks if both variables refer to the same object |
| Used for most comparisons | Commonly used with None |
May return True for different objects with equal values |
Returns True only for the exact same object |
6. = vs == vs is
=, ==, and is look similar, but they serve completely different purposes in Python.
| Operator | Type | Purpose |
|---|---|---|
= |
Assignment operator | Assigns a value to a variable. |
== |
Comparison operator | Checks whether two values are equal. |
is |
Identity operator | Checks whether two variables refer to the same object. |
=
The assignment operator stores a reference to an object in a variable. See Variables are references
Output
Here, = does not compare anything. It simply assigns a value (or object reference) to a variable.
==
The equality operator compares the values of two objects.
Output
Although a and b are different objects, they contain the same values.
is
The identity operator compares whether two variables refer to the same object.
Output
If the variables refer to different objects, is returns False, even if their values are equal.
Output
Key Takeaways
append()andextend()are list methods used to add elements, butappend()adds a single object whileextend()adds elements from an iterable.append()always adds to the end of a list, whereasinsert()adds an element at a specified position.remove()deletes an element by value,pop()removes and returns an element by index, anddeldeletes items, slices, or entire objects.sort()is a list instance method that sorts the original list, whilesorted()is a built-in function that returns a new sorted list from any iterable.==compares the values of two objects, whereasiscompares whether two variables refer to the same object in memory.=assigns a value or object reference to a variable,==compares values, andiscompares object identity.
Understanding these differences will help you write clearer Python code, avoid common beginner mistakes, and confidently answer many Python interview questions.
Summary
= |
== |
is |
|---|---|---|
| Assigns a value to a variable | Compares values | Compares object identity |
| Assignment operator | Comparison operator | Identity operator |
Does not return True or False |
Returns True or False |
Returns True or False |
| Used to create or update variables | Used for value comparison | Commonly used with None |