Python programming: How to Create a Python List

0
228
How to Create a Python List
How to Create a Python List

How to Create a Python List: If we are given a lot of data, we usually list it, right? For example, if you have many friends, you will have to declare many variables to write them separately. What we can do without doing this is to create a list of friends. I will add all the friends inside the list. Thus:

friends = ["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"] 

There are 5 friends in our friend list above. We will call these items. Now if we want to print the first item of the Friend List, we will write:

print friens[0] 

To print the second item of the Friend List:

print (frineds[1]) 

To print the third item of the Friend List:

print frineds[2] 

Here 0, 1, 2 are the indexes of these lists. The index of the list starts from 1. So we will print the last item in the list above:

print (friends[4]) 

To get the names of all the friends we will write:

print (friends) 

Which will print all your friend’s name:

['Shubo', 'Tuhin', 'Tanvir', 'Dipu', 'Siyam'] 

We can also use a negative index to get an item on the list. Using -1 will print the last item in the list. Using -2 will print the second item from the end of the list.

print (friends[-1]) 

Siyam will print when you write.

print (friends[-2])

Dipu will print like this.
If we want to access an index that is not in the list, then it returns an error. For example, there are only 5 items on our list. We can access up to 4 indexes if we want. Now if I want to access an index of 5 or greater, then I will give the error.

print (friends[10]) 

If we run the program by typing, it will give an error.

append()

I am assuming you are acquainted with a new friend. Now you want to add it to your friend list, call this append. And I’ll write like this:

friends.append("Mahir")

Then the total number of friends in your friend list is 6. If you want to see the sixth friend, print:

print (friends[5]) 

Full Program:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]

friends.append("Mahir")

print(friends[5])

Len ()

Suppose a lot of friends have been added to the list. Now that you know how many friends you have in total, we can know by the len () method.

len(friends)

If you write, we will know how many friends are on the friend list. That is, len () is used to find out how many items are in a list.

How to Create a Python List: This list is called Array in Other Programming Languages. Some of the better ones are available from the array in the list. It seems that a friend has gotten into a relationship with him, assuming with Tuhin. You don’t want to put him on the friend list anymore. Then we will remove what we do. For him:
friends.remove (‘Tuhin’)

Full Program:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
print(friends)
friends.remove('Tuhin')
print(friends)

First, we have printed all the friends of the Friendlist. Then we removed Tuhin. And after removing the print again the Friend List, I see that Tuhin is no longer on the Friend List.

del


Another way we can delete friend, delete briefly del with. Then we will use the friend’s index instead of the friend’s name. For example, Tuhin’s index is 1. To delete it, we will write:

del friends[1] 

Full Program:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
print(friends)
delfriends[1]
print(friends)

Assuming your friend list is too big. So one day you wish to have less talk, delete all of them together. The index for this is from 1 to 4. Then we will write like this:

del friends[1:4] 

What to do, remove all the friends before the index one to 4. The fourth index will not be removed. Will remove index 1, 2 and 3. If you also want to remove the friend in the fourth index, write:

del friends[1:5] 

Here 1: 5 means the range. How much We can give 2:10. So giving 2:10 removes all the fends in the index from 2 to 9.
A complete program:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
print(friends)
delfriends[1:4]
print(friends)

The contact with the last friend of the friend list is less, so you want to remove it, and put it in a variable. It can be used for pop. Like this:

  1. removed = friends.pop()

Full Program:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
removed =friends.pop()
print(removed +" removed from friend list")

No, the last friend on your friend list has been removed. With a pop, we can also remove a specific index friend, for that:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
removed =friends.pop(3)
print(removed +" removed from friend list") 

Do the above program, remove the friend in index 3 and return it and put it in the variable named removed. Then we print the removed variable.
sort ()
If you want to sort your friends by name, we’ll sort what we do. We will write like this:

friends.sort()

Then our list will be sorted/sorted so that I can easily find an item.

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
friends.sort()
print(friends)

clear()

One day you’re so angry, you don’t want to talk to anyone anymore. He wants to stop talking to everyone. Then you will clear the friend list, will you? For him:

  1. friends.clear()

You have no friends on your list anymore. 

The list we’ve been working on so far was a string list. All of those items were a string. We can also work with the number list if we want. Ex:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Which is an integer list. If we want we can access any item like the friend list above, add a new item.

One of the fun advantages of Python is that different data types can be placed on the same list. Don’t we all have a box in our house? We cover everything that is needed, like this. For example:

box = [“My name is khan”, 3.1416, 911] 

One item on this list is one type. We can also find out which type of item if you wish.

print(type (box[0])) 

As you type, it prints the item of index 0, of which type.

List Concatenation: How to Create a Python List

We can combine more than one list if we want. I can put it on another list again. As we have two lists, we want to print them together, then we will write like this:

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
box =["My name is khan", 3.1416, 999, "Jhon Doe"]
print(friends +box)

If you want again, I can put it on another list, so that I can use it later

friends =["Shubo", "Tuhin", "Tanvir", "Dipu", "Siyam"]
box =["My name is khan", 3.1416, 999, "Jhon Doe"]
new_list =friends +box

In this chapter we have learned a lot about this list. These will be useful to learn about other topics in the next chapter. So before moving on to the next section, you can work through the various lists yourself. How to do different operations of the list, you can learn better.

Note: More Info (Tech24infobd) ( How to Create a Python List)

LEAVE A REPLY

Please enter your comment!
Please enter your name here