Function Of Tuple, List, String And Dictionary In Python

This article is about the function of List, Tuple, String, and Dictionary, and will explain function with a simple example.

List function

append()

To add an item to the end of the list.

Syntax

lst.append(value)

insert()

To insert a list item at a specified index.

Syntax

lst.insert(position,value)

extend()

To append elements from another list to the current list you can add any iterable object (tuples,set,dictionary etc.)

Syntax

lst.extend(lst2)

remove()

The remove() method removes the specified item.

Syntax

lst.remove(value)

pop()

The pop() method removes the specified index. If you do not specify the index, the pop () method removes the last item.

Syntax

lst.pop(index)

clear()

The clear() method empties the list. The list still remains, but it has no content.

Syntax

lst.clear()

sort()

Sort the list alphanumerically, ascending, by default.

Syntax

lst.sort()

 

copy()

Make a copy of a list with the copy() mathod.

Syntax

lst2=lst1.copy()

reverse()

Reverse the order of the list.

Syntax

lst.reverse()

count()

Return the number of elements with the specified value.

syntax

lst.count(value)

index()

Return the index of the first elements with the specified value.

syntax

lst.index(value)

String function

capitalize()

Convert the first character to upper case.

Syntax

string_name.capitalize()

center()

Center aline the string using a specified character (space is the default)as the fill character.

Syntax

str.center(number,character)

count()

It returns the number of times a specified value appears in the string.

Syntax

str.count(value,start,end)

endswith()

The method returns true if the string ends with the specified value otherwise false.

Syntax

str.endswith(value)

expandtabs()

Set the tab size to the specified number to white spaces.

Syntax

str.expandtabs(tabsize)

find()

Finds the first occurrence of the specified

Syntax

str.find(value)

Tuple function

count()

Returns the number of times a specified value occurs in a tuple.

syntax

tup.count(value)

index()

Searches the tuple for a specified value and returns the position of where it was found.

syntax

tup.index(value)

Dictionary function

copy()

A dictionary can be copied with the method copy().

syntax

dict.copy()

update()

It matters the keys and values of one dictionary into another, overwriting values of the same keys.

Syntax

dict.update({"key":"value"})

values()

It return the list of dictionary dict's values.

Syntax

dict.values()

keys()

It returns the list of dictionary dict's keys.

Syntax

dict.keys()

items()

It returns the list of dictionary dict's keys, values in tuple pairs.

syntax

dict.items()

clear()

Removes all elements of dictionary dict.

syntax

dict.clear()

I hope you enjoy this article.