Python Lists and Tuples Operations Cheat Sheet
Lists and Tuples Operations Cheat Sheet
Lists and tuples are both sequences, so they share a number of sequence operations. But, because lists are mutable, there are also a number of methods specific just to lists. This cheat sheet gives you a run down of the common operations first, and the list-specific operations second.
Common sequence operations
- len(sequence) Returns the length of the sequence
- for element in sequence Iterates over each element in the sequence
- if element in sequence Checks whether the element is part of the sequence
- sequence[i] Accesses the element at index i of the sequence, starting at zero
- sequence[i:j] Accesses a slice starting at index i, ending at index j-1. If i is omitted, it's 0 by default. If j is omitted, it's len(sequence) by default.
- for index, element in enumerate(sequence) Iterates over both the indexes and the elements in the sequence at the same time
Check out the official documentation for sequence operations.
List-specific operations and methods
- list[i] = x Replaces the element at index i with x
- list.append(x) Inserts x at the end of the list
- list.insert(i, x) Inserts x at index i
- list.pop(i) Returns the element a index i, also removing it from the list. If i is omitted, the last element is returned and removed.
- list.remove(x) Removes the first occurrence of x in the list
- list.sort() Sorts the items in the list
- list.reverse() Reverses the order of items of the list
- list.clear() Removes all the items of the list
- list.copy() Creates a copy of the list
- list.extend(other_list) Appends all the elements of other_list at the end of list
Most of these methods come from the fact that lists are mutable sequences. For more info, see the official documentation for mutable sequences and the list specific documentation.
List comprehension
- [expression for the variable in sequence] Creates a new list based on the given sequence. Each element is the result of the given expression.
- [expression for the variable in sequence if condition] Creates a new list based on the given sequence. Each element is the result of the given expression; elements only get added if the condition is true.
list and tuples in python ppt list and tuples in python pdf list and tuples in python w3schools difference between lists and tuples python lists vs tuples python concatenate list and tuple python list and tuple in python program merge list and tuple python list of tuples python access list of tuples python append list tuple array python a list of tuples python list of tuples python create list of tuples python count list of tuples comprehension python list to tuple conversion python list to tuple converter python lists and tuples in python list and tuple difference python lists tuples dictionaries python list tuple dict python list tuple dictionary python ppt list of tuples python example python list and tuple exercises list of tuples element python list of tuples python for loop list of tuples python get first list of tuples groupby python given that python lists and python tuples are quite similar list and tuple in python in hindi list and tuple in python difference list dictionaries and tuples in python compare list and tuples in python list tuple join python list and tuples in python lists dictionaries and tuples in python list tuple and set in python list and tuples what are lists and tuples in python what is list and tuples in python list tuples and dictionary in python ppt list vs tuple performance python list of tuple pairs python python lists and tuples quiz python list and tuple questions list tuple range python list tuple python sorting list tuples sets python list of tuples python split list of tuples python string list of tuples search python list of tuples python to dict list tuple to python python lists and tuples python programs on lists and tuples programs on list and tuples in python python list vs tuple two lists into tuples python list of tuples python 3 list vs tuple python 3
Join the conversation