Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

? List Methods (For Lists)

The document provides a comprehensive overview of various methods for lists, strings, dictionaries, sets, and tuples in Python, including their descriptions and examples. Each section outlines specific methods available for manipulating these data types, such as adding, removing, and sorting elements. Additionally, it includes bonus built-in functions that are useful across multiple data types.

Uploaded by

throw82away
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)
5 views4 pages

? List Methods (For Lists)

The document provides a comprehensive overview of various methods for lists, strings, dictionaries, sets, and tuples in Python, including their descriptions and examples. Each section outlines specific methods available for manipulating these data types, such as adding, removing, and sorting elements. Additionally, it includes bonus built-in functions that are useful across multiple data types.

Uploaded by

throw82away
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/ 4

🔹 List Methods (For Lists)

Method Description Example

append(x) Adds x to the end of the list. numbers.append(4) # [1, 2,


3, 4]

extend(itera Adds elements from iterable to numbers.extend([4, 5]) #


ble) the end. [1, 2, 3, 4, 5]

insert(i, x) Inserts x at index i. numbers.insert(1, 1.5) #


[1, 1.5, 2, 3]

remove(x) Removes the first occurrence of x. numbers.remove(2) # [1, 3,


2]

pop(i) Removes and returns item at index removed = numbers.pop(1) #


i (last if not specified). removes 2

sort() Sorts list in ascending order. numbers.sort() # [1, 2, 3,


4, 5]

reverse() Reverses the list. numbers.reverse() # [3, 2,


1]

copy() Creates a shallow copy of the list. numbers_copy =


numbers.copy()

count(x) Counts occurrences of x. numbers.count(2) # 3

index(x) Finds the first index of x. numbers.index(2) # 1

clear() Removes all elements. numbers.clear() # []

max() Returns largest element. max(numbers) # 5

min() Returns smallest element. min(numbers) # 1

sum() Returns sum of elements. sum(numbers) # 15

len() Returns number of elements. len(numbers) # 5

sorted(itera Returns sorted copy of a list. sorted(numbers) # [1, 2,


ble) 3, 4, 5]
reversed() Returns an iterator for the list in list(reversed(numbers)) #
reverse order. [3, 2, 1]

enumerate() Returns an index-element pair list(enumerate(numbers))


iterator.

zip() Combines multiple iterables list(zip(a, b))


element-wise.

🔹 String Methods (For Text)


Method Description Example

upper() Converts string to uppercase. "hello".upper() # "HELLO"

lower() Converts string to lowercase. "HELLO".lower() # "hello"

strip() Removes whitespace from " hello ".strip() # "hello"


start and end.

replace(old, Replaces part of a string. "hello".replace("h", "y") #


new) "yello"

split(separato Splits into a list based on "hello world".split() #


r) separator. ["hello", "world"]

join(iterable) Joins list elements into a ' '.join(["hello", "world"])


string. # "hello world"

find(substring Returns the index of "hello".find("e") # 1


) substring.

startswith(pre Checks if string starts with "hello".startswith("he") #


fix) prefix. True

endswith(suffi Checks if string ends with "hello".endswith("lo") # True


x) suffix.

isalpha() Checks if all characters are "hello".isalpha() # True


letters.

isdigit() Checks if all characters are "123".isdigit() # True


digits.
isalnum() Checks if all characters are "hello123".isalnum() # True
alphanumeric.

🔹 Dictionary Methods (For Key-Value Pairs)


Method Description Example

keys() Returns all keys. d.keys()

values() Returns all values. d.values()

items() Returns key-value pairs. d.items()

get(key, Gets a value (without error if missing). d.get("name",


default) "Unknown")

update(dict2) Merges another dictionary. d.update({"age":


30})

pop(key) Removes and returns value for key. d.pop("name")

clear() Removes all items. d.clear()

🔹 Set Methods (For Unordered Unique Elements)


Method Description Example

add(x) Adds x to the set. s.add(4)

remove(x) Removes x (error if missing). s.remove(2)

discard(x) Removes x (no error if s.discard(2)


missing).

union(set2) Combines two sets. s1.union(s2)

intersection(s Gets common elements. s1.intersection


et2) (s2)

difference(set Gets elements not in set2. s1.difference(s


2) 2)
clear() Removes all elements. s.clear()

🔹 Tuple Methods (For Immutable Lists)


Method Description Example

count( Counts occurrences of t.count(


x) x. 2)

index( Returns first index of x. t.index(


x) 2)

BONUS: Built-in Functions Useful for Lists & More


Function Description Example

len() Returns number of items. len(numbers) # 5

max() Returns largest item. max(numbers) # 5

min() Returns smallest item. min(numbers) # 1

sum() Returns sum of all sum(numbers) # 15


elements.

sorted() Returns a sorted copy. sorted(numbers) # [1, 2,


3, 4, 5]

reversed( Returns a reversed iterator. list(reversed(numbers))


)

enumerate Returns index-value pairs. list(enumerate(numbers))


()

zip() Merges multiple iterables. list(zip(a, b))

You might also like