What if you want to store multiple values of same or different types in single variable in Python? For this Python provides container data type called List.
List are ordered and changeable (mutable) collection of elements. It can have any number of elements and the elements need not be of the same type. A list is flexible in nature i.e. a list can grow or shrink during execution of the program.
Creating a List :
To create a list, the elements are placed inside square brackets ([]), separated by commas. List can have any number of elements and the elements need not be of same type. Each element or value that is inside of a list is called an item. Look at the given example:
Lists are stored in memory exactly like strings i.e. list-elements are also indexed. List also stored with a 2-way index for each location, so, list index is Bi-Direction i.e. Forward and Backward Direction. The index of a list begin from 0 to length-1 in forward direction and -1,-2,……-length in backward direction. For example :
ITERATING/TRAVERSING A STRING :
Basically, Traversing a list means to process every item in a list, usually from left end to right end. As mentioned above, Python allows 2 ways to do this, both useful but not identical. You can traverse a list by the following ways:
Traversing by any index :
Lists are stored in ordered sequence , it is meant that every element of the list can be called individually by its index number. You can access a single item at a time by giving any index number (within list length) in square bracket. For example, if “ages” is a variable name and “[25,30,18,35,40,32,23,26]” is its value, then Python will store and access item by index like this :
Traversing by loop without indexing :
In this method, you can access each item of a list by using a For loop. In this method you will only be able to print/access the value but not the index.
Note : In this method, we don’t care about the length of list, loop will automatically handle it.
Traversing by loop with indexing :
This is also the same method as previous one, but in this we use range() function with for loop, so that with the help of range() we can easily get index number as well. This is the most common way where you can also get the index number of list and even print it.
Traversing by while loop with indexing :
You can also iterate the list in python language using a while loop.
Traversing by list comprehension :
This is the most concrete way to iterate the list while programming in the Python language. The method to iterate list using list comprehension is given below:
Traversing by Enumerate () :
- Enumerate() is python’s built-in functions which work almost like a loop to iterate an object. Instead of putting the iterable directly in the for loop, you put it inside the parentheses of enumerate().
- When you use enumerate(), the function gives back two loop variables to you:
- The count of the current iteration
- The value of the item at the current iteration
- Just like with a normal for loop, the loop variables can be named whatever you want them to be named. You can use any valid python variable’s name.
- With enumerate(), you don’t need to remember to advance the index and end of the loop, everything is automatically handled by the Python.
ACCEPTING OF ELEMENTS OF LIST BY USER:
You can accept elements from user by following ways :
By using append function.
By using eval() function
eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer. So, by using this function you will also accept list from user. However, by using this function you will need to pass “[ ]“ at the time of input.
Example :
In conclusion, lists are a fundamental data type in Python that allow for the efficient storage and manipulation of data. They are incredibly versatile and can be used in a wide range of programming contexts, from simple data storage to complex data analysis and manipulation. With the knowledge gained from this article, you should now have a solid understanding of the basics of Python lists, including how to create and modify them, and how to use them in your programs. Whether you are a beginner or an experienced programmer, mastering the power of Python lists is an essential skill that will enhance your ability to write efficient and effective code. So, start exploring the many possibilities of Python lists and take your programming skills to the next level.
In our next article we will discuss about list operators.
1 comment
Thx