Python List – How to Work with Python List

In this Python List article we are going to learn How to Work with Python List, so first of all what is List in Python ? A list is a collection which is ordered and changeable, or a list is a collection of items in a particular order. you can make a list that includes the letters of the alphabet, the digits from 0–9, You can put anything you want into a list. you can use square brackets ([]) for creating list.

 

 

 

Python List Key Features

Python lists are one of the most commonly used data structures in Python. some of the key features of Python lists are:

  1. Ordered: Python lists are ordered, which means the items in the list are stored in  particular order.
  2. Mutable: Lists are mutable, meaning they can be changed or modified after they are created. You can add, remove, or modify elements in a list.
  3. Dynamic: Lists are dynamic, meaning they can grow or shrink in size during runtime as elements are added or removed.
  4. Heterogeneous: Python lists can store elements of different data types, including integers, floats, strings, and even other lists.
  5. Indexable: Elements in a list can be accessed using their index, which starts from 0 for the first element and increases by 1 for each subsequent element.
  6. Slicing: Lists support slicing, which allows you to extract a portion of the list using the syntax list[start:stop:step].
  7. Built-in Functions: Python provides a range of built-in functions for working with lists, including len(), min(), max(), sum(), sorted(), and reversed().
  8. Versatile: Lists can be used to solve a wide range of problems, including storing and manipulating data, implementing algorithms, and building data structures.

Overall, Python lists are a flexible and versatile data structure that can be used for a wide range of tasks, making them an essential tool for Python developers.

 

 

 

 

 

Let’s create our first List in Python.

 

 

You can see that we have created a list of programming languages in Python, if you run the code

you will see the list output.

Python List - How to Work with Python List
Python List – How to Work with Python List

 

 

 

 

Accessing the Element in Python List

OK now we want to access specific element in a List, for accessing the items in python list you need to use the index of that element, and the index starts from 0, for example in here i want to access  C++, so the index for  C++ item is 2.

 

 

 

Run the code and this is the result.

Python List
Python List

 

 

 

 

Using List Item with FString 

So now we want to use fstring with list items.

 

 

 

Run the code this is the result.

How to Work with Python List
How to Work with Python List

 

 

 

Changing Item in a List

Also you can change an item in a list. so in here i want to change the first index in the list.

 

 

This is the result 

Python List
Python List

 

 

 

Adding Element to List

The simples way is using append, so when we append an item in list, the new item will be at the end of the list.

 

 

 

This is the result

Appending Items to Python List
Appending Items to Python List

 

 

 

 

Also using append you can create dynamically lists.

 

 

 

 

Inserting Element to Python List

Using insert method you can add element at any position in your list. because when you are going to use insert you can specify the index of the element.

 

 

 

This is the result

Insert Item To List
Insert Item To List

 

 

 

Removing Element in Python List

There are different ways that you can remove items from a list, you can use del if you know
the position or index of the list.

 

  • Removing with del keyword

 

 

 

This is the result 

Deleting Item From List in Python
Deleting Item From List in Python

 

 

 

  • Removing with Pop() method

The pop() method removes the last item in a list, but it lets you work with that item

after removing it

 

 

This is the result.

Pop Item List in Python
Pop Item List in Python

 

 

 

  • Removing an item by value 

For removing item by value we can use remove method.

 

 

 

 

This is the result.

Removing Item By Value
Removing Item By Value

 

 

 

 

Sorting List Permanently

For sorting the list permanently we can use sort method.

 

 

 

This is the result 

Python Sorting List
Python Sorting List

 

 

 

 

You can also sort this list in reverse alphabetical order. you can add reverse to True.

 

 

there is another function for soring lists that is called sorted(), using sorted() method
you can sort list items temporarily.

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×