Python: Data Structures & Comprehensions
125 cards · 🇺🇸 English
List/dict/set methods plus list, dict, and generator comprehensions.
Study all 125 cards for free
Flashcards, quizzes, pixel-art games and a daily review that remembers for you.
Try the first 10 cards
1 / 10List.append(x)
Tap the card for the answer · swipe right if you knew it, left if not
Already have an account? Sign in
Cards
- 1List.append(x)Adds an item to the end of the list (similar to a[len(a):] = [x])
- 2List.extend(iterable)Extends the list by appending all items from the iterable (similar to a[len(a):] = iterable)
- 3List.insert(i, x)Inserts an item at a given position (index i). a.insert(0, x) inserts at front, a.insert(len(a), x) is equivalent to append
- 4List.remove(x)Removes the first item whose value equals x. Raises ValueError if no such item
- 5List.pop([i])Removes and returns the item at position i. Without index, removes and returns the last item. Raises IndexError if list is empty or index out of range
- 6List.clear()Removes all items from the list (similar to del a[:])
- 7List.index(x[, start[, end]])Returns zero-based index of first occurrence of x. Raises ValueError if no such item. Optional start/end limit the search
- 8List.count(x)Returns the number of times x appears in the list
- 9List.sort(*, key=None, reverse=False)Sorts the items of the list in place
- 10List.reverse()Reverses the elements of the list in place
- 11List.copy()Returns a shallow copy of the list (similar to a[:])
- 12List methods like insert, remove, sortNone - they modify the list in place and return the default None (design principle for all mutable data structures in Python)
- 13Use a list as a stackUse append() to add to top, pop() without index to retrieve from top (last-in, first-out)
- 14Why are lists not efficient as queues?Appends/pops from end are fast, but inserts/pops from beginning are slow (all elements must shift by one)
- 15What should you use instead of lists for queues?collections.deque which has fast appends and pops from both ends
- 16List comprehension syntax[expression for item in iterable if condition] - creates a new list
- 17Equivalent of [x**2 for x in range(10)]list(map(lambda x: x**2, range(10))) or a for loop with append
- 18Must tuple expressions in list comprehensions be parenthesized?Yes - [(x, y) for x in range(3)] works, but [x, y for x in range(3)] raises SyntaxError
- 19Can list comprehensions be nested?Yes - the initial expression can be another list comprehension
- 20del a[i] vs a.pop(i)del removes the item; pop removes and returns it
- 21Del a[:]Clears the entire list (same as a.clear())
- 22Can del delete entire variables?Yes - del a deletes the variable a
- 23Are tuples mutable or immutable?Immutable - you cannot assign to individual items
- 24Can tuples contain mutable objects?Yes - e.g., v = ([1, 2], [3, 4]) is valid
- 25Create an empty tupleUse empty parentheses: ()
- 26Create a tuple with one itemFollow the value with a comma: ('hello',) - parentheses alone are not sufficient
- 27Tuple packingt = 12345, 54321, 'hello!' - values are packed together in a tuple
- 28Sequence unpackingx, y, z = t - assigns each element of the sequence to a variable (requires same number of variables as elements)
- 29Set in PythonAn unordered collection with no duplicate elements
- 30Create an empty setUse set() - not {} which creates an empty dictionary
And 95 more cards. Study the whole deck free
Articles about this deck
More Programming decks
See all- 🐍
Python: OOP Fundamentals
59 cards · 🇺🇸 English
Classes, inheritance, and core object-oriented Python concepts.
- 🐍
Python : avancé (décorateurs, lambda et exceptions)
97 cards · 🇺🇸 English
Décorateurs, fonctions lambda, gestion des exceptions et autres concepts Python avancés.
- 🐍
Python : structures de données et compréhensions
125 cards · 🇺🇸 English
Méthodes de listes/dictionnaires/ensembles ainsi que les compréhensions de listes, de dictionnaires et de générateurs.
- 🐍
Python: Fortgeschritten (Decorators, Lambda & Exceptions)
97 cards · 🇺🇸 English
Decorators, Lambda-Funktionen, Exception-Handling und weitere fortgeschrittene Python-Konzepte.
- 🐍
Python: Funciones integradas
77 cards · 🇺🇸 English
Funciones integradas esenciales que todo desarrollador Python debería conocer.
- 🐍
Python : fondamentaux de la POO
59 cards · 🇺🇸 English
Classes, héritage et concepts fondamentaux de la programmation orientée objet en Python.
