reduhq / data_structures.py

Data structures in python (Lists, Dictionaries, Tuples, Sets)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CONTENT

DATA STRUCTURES

  1. Lists:

    • Ordered sequence of elements.
    • Mutable: You can add, remove, or modify elements.
    • Defined using square brackets [ ].
    • Allows duplicates.
  2. Tuples:

    • Ordered sequence of elements.
    • Immutable: Once created, they cannot be modified.
    • Defined using parentheses ( ).
    • Allows duplicates.
  3. Dictionaries:

    • Associative data structure that stores key-value pairs.
    • Mutable: You can add, remove, or modify elements.
    • Defined using curly braces { }.
    • Does not allow duplicate keys, but values can be repeated.
  4. Sets:

    • Unordered collection of unique elements.
    • Mutable: You can add or remove elements.
    • Defined using curly braces { } or the set() function.
    • Does not allow duplicate elements.

METHODS

STRING METHODS

NOTE: To see more list methods, visit Python String Methods

Method Description example
DIR The dir() method returns the list of valid attributes of the passed object dir(object)
UPPER The upper() method returns a string where all characters are in upper case my_string.upper()
LOWER The lower() method returns a string where all characters are lower case my_string.lower()
CAPITALIZE The capitalize() method returns a string where the first character is upper case, and the rest is lower case my_string.capitalize()
FIND The find() method finds the first occurence of the specified value. The find() method returns -1 if the value is not found. my_string.find("welcome")
INDEX The index() method finds the first occurence of the specified value. The index() method rises an exception if the value is not found my_string.("hello")
ISNUMERIC The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False my_string.isnumeric()
ISALPHA The isalpha() method returns True if all the characters are alphabeth letters (a-z). Example of characters that are not alphabet letters: (space)!#%&? etc. my_string.isalpha()
COUNT The count() method returns the number of times a specified value appears in the string my_string.count("apple")
LEN len() is a function that returns an integer which is the length of the string len(my_string)
STARTSWITH The startswith() method returns True if the string starts with the specified value, otherwise False my_string.startswith("Hi")
ENDSWITH The endswith() method returns True if the string ends with the specified value, otherwise False my_string.endswith(".")
REPLACE The replace() method replaces a specified phrase with another specified phrase my_string.replace("bananas", "apples")
SPLIT The split() method splits a string into a list my_string.split(", ")

LIST METHODS

# LIST Creates a list
list(["hello", True, 25])
my_list = ["hello", True, 25]

NOTE: To see more list methods, visit Python List Methods

Method Description example
LEN The len() function returns the number of items in a list len(my_list)
APPEND The append() method appends an element to the end of the list my_list.append("hahaha")
INSERT The insert() method inserts the specified value at the specified position my_list.insert(2, "apple")
EXTEND the extend() method adds the specified list elements (or any iterable) to the end of the current list my_list.extend([False, 20])
POP The pop() method removes the element at the specified position my_list.pop(0)
REMOVE The remove() method removes the specified item my_list.remove("apple")
CLEAR The clear() method removes all the elements from a list my_list.clear()
SORT The sort() method sorts the list ascending by default my_list.sort(), my_list.sort(reverse=True)
REVERSE The reverse() method reverses the sorting order of the elements my_list.reverse()

DICT METHODS

# DICT Creates a dictionary
dict(name="Liam", age=25, city="Madrid")
my_dict = {
	"name": "Liam",
	"age": 25,
	"city": "Madrid"
}

NOTE: To see more dict methods, visit Python Dictionary Methods

Method Description example
KEYS The keys() method returns a view object. The view object contains the keys of the dictionary, as a list. my_dict.keys()
GET The get() method returns the value of the item with the specified key, if the key doesn't exist, returns None my_dict.get("name")
CLEAR The clear() method removes all items from the dictionary my_dict.clear()
POP The pop() method removes the specified item from the dictionary my_dict.pop("name")
ITEMS the items() method is used to return the list with all dictionary keys with values my_dict.items()

SET METHODS

# SET creates a set
set(["Luis", "Jilliam"])
my_set = {"Luis", "Jilliam"}

# Creating an inmutable set
frozenset(["Luis", "Jilliam"])

NOTE: To see more set methods, visit Python Set Methods

Method Description example
ISSUBSET Return True if all items in set x are present in set y my_set_x.issubset(my_set_y)
ISSUPERSET Return True if all items set y are present in set x my_set_x.issuperset(my_set_y)
ISDISJOINT Return True if no items in set x is present in set y my_set_x.isdisjoint(my_set_y)

About

Data structures in python (Lists, Dictionaries, Tuples, Sets)