List [] is a collection of elements that is ordered and mutable. they can contain elements of different
data types, including integers, floats, strings, and even other lists.
Lists are mutable, meaning that elements can be added, removed, or modified after the list is created.
Lists support various operations, such as concatenation (+), repetition (*), and membership testing (in).
Comprahension : -
clear() : method removes all items from the List making it an empty/null list.
count() : method returns the count of the occurrences of a given element in a list.
Extend(): method adds items of an iterable (list, tuple, dictionary, etc) at the end of a list.
index(): searches for a given element from the start of the list and returns the position of the first occurrence.
reverse() : is an inbuilt method in the Python programming language that reverses objects of the List in place i.e.
it doesn’t use any extra space but it just modifies the original list.
sort(): method sorts the elements of a list. It sorts in ascending order by default but can also sort values in
descending order or in a custom manner using its parameters.
min(): function returns the smallest of the values or the smallest item in an iterable passed as its parameter
max(): function returns the largest item in an iterable or the largest of two or more arguments.
Dictionary Methods
dictionary {} is a collection of key-value pairs that is unordered and mutable, meaning that key-value
pairs can be added, removed, or modified after the dictionary is created.
Individual elements of a dictionary can be accessed using square bracket notation [] with the key.
Comprahension:
{key_expression: value_expression for item in iterable if condition}
fromkeys(): function returns the dictionary with key mapped and specific value. It creates a new dictionary from
the given sequence with the specific value.
seq = ('a', 'b', 'c') print(dict.fromkeys(seq, None)) O/P : {'a': None, 'b': None, 'c': None}
get(): Method return the value for the given key if present in the dictionary. If not, then it will return None. ex: d
= {'coding': 'good', 'thinking': 'better'}print(d.get('coding')) O/P: good
keys(): Method returns a view object that displays a list of all the keys in the dictionary .
pop(): method removes and returns the specified element from the dictionary.
popitem(): method removes the last inserted key-value pair from the dictionary and returns it as a tuple.
setdefault(): returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to
the dictionary.
d = {'a': 97, 'b': 98} -- print("setdefault() returned:", d.setdefault('b', 99)) -- print("After using setdefault():", d)
O/P: setdefault() returned: 98 -- After using setdefault(): {'a': 97, 'b': 98}
update(): method updates the dictionary with the elements from another dictionary object or from an
iterable of key/value pairs.
String Methods
string is a sequence of characters enclosed within either single quotes (') or double quotes ("). Strings
are immutable, meaning they cannot be modified after creation.
capitalize(): Converts the first character of the string to a capital (uppercase) letter
rindex(): Returns the highest index of the substring inside the string. (from right)
isalnum(): Checks whether all the characters in a given string is alphanumeric or not
isnumeric(): turns “True” if all characters in the string are numeric characters
isprintable(): Returns “True” if all characters in the string are printable or the string is empty
isspace(): Returns “True” if all characters in the string are whitespace characters
list1 = {'1', '2', '3', '4', '4'} s = "-#-" s = s.join(list1) o/p = "1-#-3-#-2-#-4
partition(): Splits the string at the first occurrence of the separator and returns in tuple.
str = "I love Geeks for geeks" print(str.partition("for")) O/P: ('I love Geeks ', 'for', ' geeks')
split(): method is used to split a string into a list of substrings based on a delimiter.
rsplit(): Split the string from the right by the specified separator
print (string.splitlines( ))