Append in Python

Learn to append in Python
Learn to append in Python

The append() method in python adds a single item to the existing list. It doesn’t return a new list of items but will modify the original list by adding the item to the end of the list

Every programming language has a certain number of built-in data structures. Data structures are basically structures that hold the data together so that we can use it to our advantage.

There are four built-in structures in Python:

List: It is a data structure in python which is ordered and mutable(changeable). A list allows duplicate members.

Dictionary: It is a data structure in python which is unordered, indexed and mutable. Dictionary does not allow duplicate members.

Tuple: It is a data structure in python which is ordered and unchangeable. Tuple allows duplicate members.

Set: It is a data structure in python which is unordered and unindexed. Set does not allow duplicate members.

Properties of List in Python:

  • Lists are ordered: It means that lists that have the same members but different order or position are not the same.
  • List elements can be accessed by index value: We can fetch the element at a specific index in the list.
  • Lists are mutable: We can change or modify the elements in lists even after it has been created.
  • Lists can be nested: There can be other lists inside of a list.
  • Lists may contain arbitrary objects: There can be elements of several types in a single list. It means there can be float, integer and string values in a single list.

( A ) Creating a list:

a1. Create an empty list

a2. Create a list with integers only

a3. Create a list with mixed data type

We can add elements with multiple data types in list.

a4. Create a nested list

( B ) Accessing elements from a list:

b1. Accessing list elements with index value

b2. Index value must be integer

b3. We can’t access lists by string values

b4. Accessing nested list elements

b5. Accessing elements with negative index

Understanding Indexing In Python:

( C ) Slicing a list:

In slicing operation the element at stop index is not included.

c1. slicing a list with a positive index

c2. slicing a list with negative index

( D ) Changing the elements of a list:

d1. Change a single element

d2. Change multiple elements of a list

Python list methods:

( A ) append(): Add an element to the end of the list:

a1. Appending a single element

a2. Appending a list

Here we can see that the entire list is appended to the list, instead of each element.

a3. Appending a tuple

a4. Appending a set

( B ) extend() : Add all elements of a list to another list:

b1. Extending elements of a list

Here we can see that each element of the list is added to the list, instead of the entire list.

b2. Extending elements of a tuple

b3. Extending elements of set

b4. Extending using + operator

Extend() vs Append():

( C ) Insert() : insert an element at a specific index:

c1. Insert an element at 0th index

c2. Insert an element at 2nd index

c3. Insert a list

c4. Insert a tuple

c5. Insert a set

( D ) Remove() : remove an item from the list:

d1. Remove an element

d2. Removing an element with duplicate values

d3. Remove an element that doesn’t exist

( E ) pop() : It removes and returns element at the given index:

e1. Popping an element from a specified index

e2. Popping an element without index.

It deletes the last element from the list

e3. Popping an element with negative index

( F ) clear() : removes all items from a list.

f1. Using clear()

f2. Using del to clear the list

f3. Delete elements by assigning an empty list

( G ) index() : It returns the index of first matched item.

g1. Element present in the list

g2. Element not present in the list

g3. Using start index

It will start finding the element from the starting index. Default starting index is 0.

g4. Using start and end indexes

It will find the element within the given range.

( H ) count() : It returns the count of item passed:

h1. Count the number of element

h2. Count the tuple elements

( I ) sort() : sort items in ascending order:

i1. Sorting in ascending order

It changes the order of the original list.

i2. Sort in descending order

It changes the order of the original list.

i3. Using sorted

It does not change the order of the original list.

( J ) reverse() : It reverse the order of the items in the list:

j1. Reversing a list

It changes the original list.

j2. Reverse list using slicing operator

It doesn’t change the order of the original list.

j3. Using reversed() function

( K ) copy() : It returns copy of a list:

k1. Copying a list.

The main problem with this method is that, after copying the list, if we modify any of the list, both the list adept the change.

k2. Problem with previous method

Here the original list is also modified.

k3. Using copy() function

By using the copy() function we can overcome the problem mentioned above.

k4. Using slicing operation to copy a list

( L ) List membership test:

By using this method we can know whether an element is present in the list or not.

( M ) Iterating through a list:

Run and download this code online on Google Colaboratory.

To learn more about Python, visit here.

By Pratik Shukla