Python Data Structures
Lists, Sets, Dictionaries, and Tuples
In Python, there are several built-in data structures that serve different purposes and have distinct characteristics. Understanding these data structures and their functionalities is crucial for effective programming. Let's delve into the comparisons of four fundamental data structures: lists, tuples, sets, and dictionaries.
By understanding the characteristics and functionalities of these data structures, you can make informed decisions about when and how to use them in your Python programs.
Below you can find my CheatSheet and descriptive explanations for each. Have fun reading and don't miss the Conclusion at the end.
LIST
? Some of the commonly used built-in methods for lists in Python.
1) append(): Adds an element to the end of the list.
2) remove(): Removes the first occurrence of a specified value from the list.
3) pop(): Removes and returns the element at the specified index. If no index is specified, it removes and returns the last element.
4) insert(): Inserts an element at the specified index.
5) extend(): Extends the list by appending elements from the iterable.
6) count(): Returns the number of occurrences of a specified value.
7) index(): Returns the index of the first occurrence of a specified value.
8) Slicing and Item Assignment: You can access each element by slicing and assign/update with new values.
SET
? Some of the commonly used built-in methods for sets in Python.
1) add(): Adds an element to the set. If the element is already present, the set remains unchanged.
2) remove(): Removes a specified element from the set. If the element is not present, it raises a KeyError.
3) discard(): Removes a specified element from the set, if it is present. If the element is not present, it does nothing.
4) pop(): Removes and returns an random/arbitrary element from the set. If the set is empty, it raises a KeyError.
领英推荐
5) clear(): Removes all elements from the set.
6) union(): Returns a new set containing all unique elements from both sets.
7) intersection(): Returns a new set containing common elements from both sets.
8) No Slicing and No Item Assignment.
DICTIONARY
? Some of the commonly used built-in methods for dictionaries in Python.
1) len(): Returns the number of key-value pairs in the dictionary.
2) keys(): Returns a view object that displays a list of all the keys in the dictionary.
3) values(): Returns a view object that displays a list of all the values in the dictionary.
4) items(): Returns a view object that displays a list of key-value tuple pairs.
5) get(): Returns the value of the specified key. If the key does not exist, it returns a default value (default is None).
6) update(): Updates the dictionary with the key-value pairs from another dictionary or iterable.
7) Slicing and Item Assignment: You can access each element by slicing and assign/update with new values.
TUPLE
? Some of the commonly used built-in methods for tuples in Python.
1) count(): Returns the number of occurrences of a specified value in the tuple.
2) index(): Returns the index of the first occurrence of a specified value in the tuple.
3) Slicing but No Item Assignment: You can reach the elements only.
Conclusions: