Python Newbie?

Adnan Khan
3 min readNov 28, 2019

--

Day 4

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

prices=["Apple",90,"Mango",70,"Banana",45]

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.

prices=[["Apple",90],
["Mango",70],
["Banana",45]]

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.

prices=["Apple",90,"Mango",70,"Banana",45]
prices[0] #output="Apple"
prices[3] #output=70
price[-1] #output=45
price[-3] #output=70

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[3] 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].

prices[1:3] #[90,"Mango"]
prices[2:5] #["Mango",70,"Banana"]
prices[:3] #["Apple",90,"Mango",70,"Banana",45]
prices[4:] #["Banana",45]

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:

prices=[["Apple",90],
["Mango",70],
["Banana",45]]
prices[1][0] #output="Mango"
prices[2][1] #output=45

List Manipulation:

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 =prices+[["strawberry",110]]
del(final_prices[3])
final_prices[1][1] = 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.

--

--

Adnan Khan

Data Scientist who loves to teach machine through data.