# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <small><small><i>
# All the IPython Notebooks in this lecture series by Dr. <NAME> are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/003_Python_List_Methods)**
# </i></small></small>

# # Python List `pop()`
#
# The **`pop()`** method removes the item at the given index from the list and returns the removed item.
#
# **Syntax**:
#
# ```python
# list.pop(index)
# ```

# ## `pop()` Parameters
#
# * The **`pop()`** method takes a single argument (index).
# * The argument passed to the method is optional. If not passed, the default index **-1** is passed as an argument (index of the last item).
# * If the index passed to the method is not in range, it throws **IndexError: pop index out of range exception**.

# ## Return Value from `pop()`
#
# The **`pop()`** method returns the item present at the given index. This item is also removed from the list.

# +
# Example 1: Pop item at the given index from the list

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item
return_value = languages.pop(3)
print('Return Value:', return_value)

# Updated List
print('Updated List:', languages)
# -

# >**Note:** Index in Python starts from 0, not 1.
#
# If you need to pop the 4th element, you need to pass **3** to the **`pop()`** method.

# +
# Example 2: pop() without an index, and for negative indices

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 
print('Return Value:', languages.pop())
print('Updated List:', languages)

# remove and return the last item
print('\nWhen -1 is passed:') 
print('Return Value:', languages.pop(-1))
print('Updated List:', languages)

# remove and return the third last item
print('\nWhen -3 is passed:') 
print('Return Value:', languages.pop(-3))
print('Updated List:', languages)
# -

# * If you need to remove the given item from the list, you can to use the **[list remove() method](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List_Methods/005_Python_List_remove%28%29.ipynb)**.
#
#
# * And, you can use the **`del`** statement to **[remove an item or slices from the list](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List_Methods/Python_del_statement.ipynb)**.



StarcoderdataJupyterScriptsDedupFiltered