Lets get started with some hot coding in cool weather.Today we’re going to discuss builtin data structure known as List.Suppose you’re buying some fruits for your family from market and want to store fruit names and prices every time in a different variables with fruit names and store the price of that fruit in it so that would be kind of inconvenient right?.To overcome this problem Lists were introduce to store all the values in a single list.You can build such a list with square brackets like this
In simple words you can say that assigning multiple values to a single variable.It’s some how related to Arrays but the difference between array and list is that Array can store multiple values of similar type where list can store values of any type.Moreover,we can add list inside list as well which is know as sublist.
In above example you can see how we have created three sublists in a list.Now you’re familiar with how to create a list in python right. So now the question raised is how to access that values after creating the list.Let’s take a look with example first.
In the example you can notice that how can we access elements of list by mentioning their index number inside square brackets with name of the list we have given.Like C++,Python indexing also starts from zero.However,in python you can access element through negative index as well,you can notice that price and price[-3] has the same value 70.What if you want to access multiple values of the list? Yes you can access multiple values at a time through slicing as well.While specifying a slice you enter two index numbers one as starting index while other as an ending index separated by adding colon between them[start : end].
That’s how you can do slicing. where in 3rd line you can notice that there is no starting point it means start from index 0 and go till index 3 similarly in 4th line ending point is missing so it means go till last element of the list. Where in list of list you can access element like this:
we can update the list at our will like adding or removing elements from list and we can replace lists values as well, for updating a value we simply use assignment operator where for removing a value from list we use del(variable_name) function.
final_prices = 120
In first line of code we add another sublist to list prices and stored it in final_prices,for deletion of that sublist we have added we use del function and to update the value we select an element then assigned it a new value.There is one point to note down we we copy a list and assign it to another variable let’s suppose you have list A and you assign it to variable B so making change in B will also effect list A because we assign a reference of list A memory to variable B.However if you want to avoid this then you do something like this B=list(A),now if make changes in B it wouldn’t effect list A.
That’s enough for today otherwise it would be overloaded and your brain wouldn’t consume that all at once. By the way, that was kind of joke don’t forget to solve examples on lists because the more you code the more your concepts would become clear.Share your feedback or the problem you faced i’m here to make it clear for you and for me as well. Good Luck with that! See you tomorrow with another builtin data structure of Python.