Python Data Structures
Python_Data_Structures

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.


Comparison of List, Set, Dictionary and Tuple

LIST

  • Square Bracket: [...]
  • Ordered: Ordered as created.
  • Mutable: It can be changed and manipulated.

? 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

  • Curly bracket: {...}
  • Unordered: In Python, sets are implemented using a data structure called a hash table or hash set. The elements of a set are stored based on their hash values, which are determined by the hash function. The order of elements in a set is not based on their insertion order or any inherent ordering of the elements themselves. Instead, the elements are stored in an order that optimizes the performance of set operations like membership testing, intersection, union, and so on.
  • Mutable: It can be changed and manipulated.

? 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

  • Curly bracket: {...} but key-value pairs.
  • Unordered: Unordered collections of key-value pairs.
  • Mutable: They are mutable, meaning you can add, remove, or modify key-value pairs after the dictionary is created.

? 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

  • Parenthesis: (...)
  • Ordered: Ordered as created.
  • Immutable: Tuples are immutable in Python, meaning once they are created, their contents cannot be changed. Because of this immutability, tuples have fewer built-in methods compared to lists.

? 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:

  • Choose Wisely: Understand the characteristics of lists, tuples, sets, and dictionaries to select the appropriate data structure for your needs.
  • Mutability Matters: Lists are mutable, tuples are immutable; choose based on whether your data needs to change.
  • Unique Elements: Sets are ideal for storing unique elements and performing set operations efficiently.
  • Key-Value Mapping: Dictionaries excel at mapping keys to values and fast data retrieval.
  • Efficiency Considerations: Consider efficiency, readability, and specific requirements when selecting a data structure.
  • Immutability and Safety: Tuples and immutable structures provide safety and prevent unintended data modification.
  • Flexibility: Python's built-in data structures offer flexibility and adaptability to solve various programming tasks efficiently.

要查看或添加评论,请登录

Emirhan B.的更多文章

  • Data / Web Scraping

    Data / Web Scraping

    Hello my dear connections! In this post of Merlin's Science, I will give you information about Data Scraping and show…

    1 条评论
  • Zero - Pro - Hero (Tips and Tricks)

    Zero - Pro - Hero (Tips and Tricks)

    Introductory As I promised in my previous article, I will tell you about trending career opportunities today…

    7 条评论
  • First Step To Professional Career

    First Step To Professional Career

    Without overwhelming you with the introductory part of the article, let's start with the parts that will be useful…

    3 条评论

社区洞察

其他会员也浏览了