List in the Python programming language

List in the Python programming language, a list is a collection of items. In this Python tutorial, you will learn how to use the List function in Python. You will have a complete understanding of List by the end of this course.

In Python, a list is made by putting all items inside the square bracket [] and separating them with a comma (,).

The list’s most powerful feature is that it has no size limit and can contain items of many data types.

Stringr in r 10 data manipulation Tips and Tricks »

Empty list

List = []   

List with integer values

List = [5,6,7,8,9]

List with different Data types

List = [51, 17.2, "Jim"]   

How can I turn a string into a List?

This section was covered in the last string tutorial. Take a look at the following example:

List = "Welcome to finnstats"
List.split(' ')
'Welcome', 'to', 'finnstats']

How do I go to a certain item in a List?

The index of a List element starts at 0 and goes up from there, just like it does in a string. If we want to get to a certain item in the list, we perform the same thing we did with the string.

apply family in r apply(), lapply(), sapply(), mapply() and tapply() »

List = [10,12,13,14,15,16]
List[2]
13
List[5]
16

Negative Indexing

Negative Indexing is a method of gaining access to an element.

The backward direction in a list begins with -1, -2, and so on, just as it does in a string. Let’s take a closer look.

List = [11,12,13,14,15,16]
List[-1]
16

Is it possible to slice a list?

Slicing is the process of removing portions of a component from its entirety. In Python, we can do the same thing with List.

Slicing is a technique for extracting only a few elements from a list. The steps are the same as they were in the string.

So, are you prepared to learn how to slice? Let’s get this party started.

List = [1,2,3,4,5,6,7,8]
List[0:4]
[1,2,3,4]

It takes elements from index 0 to 3, but not from index 4 onwards.

Wil will demonstrate how each item is issued an index number.

pipe operator in R-Simplify Your Code with %>% »

List = [1, 2, 3, 4, 5, 6, 7, 8]
    index= 0  1  2  3  4  5  6  7      

We hope you now have a firm grasp of the concept of list indexing.

How do I join two lists together?

If you want to combine two lists into one, you can do so easily by using the ‘+’ operator. That’s all there is to it. Let’s have a look at an illustration.

List1 = [11,12,13,14,15]
List2 = [16,17,18,19,20]
List3 = List1 + List2
List3
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Are there any items on the List that need to be updated?

If you create a list and then need to change some of the items in the list, you can easily do so by simply giving an index value for the index location you wish to update with a new item.

Let’s have a look at an example:

List=[1,2,3,4,5,6]

Replace the value of index 0 with a new value

List[0]=9       
List
[9,2,3,4,5,6]

What is the best way to append to a list?

The append() method can be used to add a new item to a list that already exists. Simply send the value you want to append to your list to the append method.

List=[1,2,3,4,5,6]

Append element 7 in List[]

List.append(7)
List
[1,2,3,4,5,6,7]

How can I remove items from a list?

If you don’t want to use some of the items in your list, you can use the pop() method to remove them from the list. You don’t need to give a value to the pop() method because it deletes the final element from your list by default.

Sentiment analysis in R » Complete Tutorial »

List=[1,2,3,4,5,6]

To eliminate the last element in your list, which is 6 in this List[], use the pop() method.

List.pop()
List
[1,2,3,4,5]

But what exactly is it? Don’t worry if you want to remove certain specific things from your list that are in the middle of it. Python offers one more way for this operation, which is delete ()

You can delete any item from your list with the remove() method, regardless of its position in the list.

List= [1,2,3,4,5,6,7]

remove item 4 from your List

List.remove(4)
List
[1, 2, 3, 5, 6, 7]

Guys, there’s one more issue! It fails if there are two identical numbers in your list and you want to eliminate both of them.

Only one item is removed with this remove() function.

Let’s look at an example to see if the list has two identical integers.

List=[1,2,3,4,5,6,4]
List.remove(4)
List
[1, 2, 3, 5, 6, 4]

It just deletes item 4 once, because it is the first item from index 0. -Don’t worry, there’s still a way to delete the item you want to delete, regardless of how many times it appears in a list.

Suppose you don’t want to delete 4 at index 3 of List=[1,2,3,4,5,6,4,8,9], but you do want to delete 4 at index 6, thus you can use the del() method, which deletes an element of your choice.

List=[1,2,3,4,5,6,4,8,9]
del(List[6])
List
[1,2,3,4,5,6,8,9]

The del() method has the following syntax: del (name of List[index position of item])

Making a List Inside a List

If you wish to create a list inside another list, simply pass the name of the list you want to include in the new list.

List 1

Col=['red','green','yellow']

List 2

Fruits=['apple','banana','cherry']

List 3 with List 1 and List 2

num= [Colors,Fruits,[1,2,3,4,5]]
num
[['red', 'green', 'yellow'], ['apple', 'banana', 'cherry'], [1, 2, 3, 4, 5]]

Colors is a list, and Fruits is a list, and we add a third list as num. We pass both the Colors and Fruits lists, as well as the num list’s own items, in the num list. And they’re all grouped together as a num list.

As you can see in the example, several data types of items are contained in one list. The fact that we may store diverse data types in one list is the list’s main feature.

By writing the lines below, we may get a specific list from the num list:

num= [Colors,Fruits,[1,2,3,4,5]]
num[0]
['red', 'green', 'yellow']    #which is List Colors at index 0

By writing the following line-by-line, we may get any specific element from the list.

Filtering Data in R 10 Tips -tidyverse package »

num= [Colors,Fruits,[1,2,3,4,5]]
num[2][1]
2

In num[2][1]-

The index location of the List is represented by [2].

The index of the entry within the list is represented by [1].

That’s all there is to List in Python; hopefully, you now have a better understanding of the concept.

congratulations! You’ve mastered Python’s List command.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + eight =