0% found this document useful (0 votes)
4 views1 page

python_methods_cheatsheet

The document outlines built-in methods for Python data structures: lists, tuples, sets, and dictionaries. It provides examples of various operations such as adding, removing, and accessing elements for each data type. Each section highlights the characteristics of the data structures, including their order, mutability, and allowance for duplicates or unique elements.

Uploaded by

Ankit Harsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

python_methods_cheatsheet

The document outlines built-in methods for Python data structures: lists, tuples, sets, and dictionaries. It provides examples of various operations such as adding, removing, and accessing elements for each data type. Each section highlights the characteristics of the data structures, including their order, mutability, and allowance for duplicates or unique elements.

Uploaded by

Ankit Harsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Built-in Methods: List, Tuple, Set, Dictionary

LIST METHODS (Ordered, Mutable, Allows Duplicates)


--------------------------------------------------
my_list = [1, 2, 3, 2]
my_list.append(4) # [1, 2, 3, 2, 4]
my_list.extend([5, 6]) # [1, 2, 3, 2, 4, 5, 6]
my_list.insert(1, 10) # [1, 10, 2, 3, 2, 4, 5, 6]
my_list.remove(2) # [1, 10, 3, 2, 4, 5, 6]
my_list.pop() # removes last element
my_list.index(3) # returns index of 3
my_list.count(2) # count of 2
my_list.sort() # sort ascending
my_list.reverse() # reverse order
my_list.copy() # shallow copy

TUPLE METHODS (Ordered, Immutable, Allows Duplicates)


------------------------------------------------------
my_tuple = (1, 2, 3, 2)
my_tuple.count(2) # 2
my_tuple.index(3) # 2

SET METHODS (Unordered, Mutable, Unique Elements)


-------------------------------------------------
my_set = {1, 2, 3}
my_set.add(4)
my_set.update([5, 6])
my_set.remove(2)
my_set.discard(10)
my_set.pop()
my_set.union({7, 8})
my_set.intersection({3, 4})
my_set.difference({3, 4})
my_set.symmetric_difference({3, 4})
my_set.issubset({1, 2, 3, 4})
my_set.issuperset({1, 2})
my_set.isdisjoint({7, 8})

DICTIONARY METHODS (Ordered, Mutable, Keys Unique)


--------------------------------------------------
my_dict = {"name": "Alice", "age": 25}
my_dict.get("name")
my_dict.keys()
my_dict.values()
my_dict.items()
my_dict.update({"city": "Delhi"})
my_dict.pop("age")
my_dict.setdefault("gender", "F")
dict.fromkeys(["a", "b"], 0)
my_dict.clear()

You might also like