How to Copy a List in Python: Shallow and Deep Copy
See every way to copy a Python list, why plain assignment fails, and when you need copy.deepcopy() for nested lists.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
See every way to copy a Python list, why plain assignment fails, and when you need copy.deepcopy() for nested lists.
Call my_dict.clear() to empty a dictionary in place. Reassigning to {} creates a new object and leaves other references untouched.
Call my_list.clear() to empty a list in place. Reassigning to [] makes a new list and leaves other references holding the old items.
Use dict1 | dict2 on Python 3.9+ or {**dict1, **dict2} on older versions. Learn update(), ChainMap, conflict rules and deep merging.
Use + for a new list, extend() to append in place, or unpacking and itertools.chain for several lists. Compare the methods with working examples.
Use list.pop() to remove and return an element by index, handle IndexError, build stacks, and know when to use deque, remove or del instead.
Python offers remove(), pop(), del and list comprehensions for deleting list items. Learn which to use by value, index or condition, and the loop trap to avoid.
Use list.insert(index, value) to add one item, slice assignment for several, and slicing to build a new list, with edge cases and costs.
Use list.index(), enumerate() and next() to find one or all positions of a value in a Python list, and handle missing values without errors.
Learn the best ways to count how often each item appears in a Python list, from collections.Counter to plain dictionaries and pandas.
Join a list of strings in Python with separator.join(list), convert non-string items, clean values in a generator and avoid loop concatenation.
Split a Python string into a list with split(), limit the result with maxsplit, split from the right with rsplit(), or use re.split() for pattern-based delimiters.
Use random.shuffle() to reorder a list in place or random.sample() to get a shuffled copy, with tips on seeding, tuples and secure randomness.
Use list(dict.fromkeys(items)) to drop duplicates and keep order, or set() when order is irrelevant. Includes handling of unhashable items like lists.
Use if not my_dict to test for an empty Python dictionary. Compare it with len() and equality checks and learn how to handle None values.
Use if not my_tuple to test for an empty Python tuple. Compare len() and equality checks and avoid the single-element tuple trap.
Use if not my_string to test for an empty Python string. Learn how strip() handles whitespace-only input and how to guard against None.
Use if not my_list to test for an empty Python list. Learn why any() is the wrong test and how to handle None and NumPy arrays.
Calculate the mean of a Python list with sum() and len(), the statistics module or NumPy, and avoid errors with empty lists and mixed data.
Use sum(my_list) to total the numbers in a Python list. Learn the start value, float precision with math.fsum, filtered sums and common mistakes.