Write a list with 4 elements that could be passed to this function to get it to return the integer 2

9. Lists¶

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.

11. Lists¶

A list is an ordered collection of values. The values that make up a list are called its elements, or its items. We will use the term element or item to mean the same thing. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can be of any type. Lists and strings — and other collections that maintain the order of their items — are called sequences.

Python Lists

Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.

List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility.

Note- Lists are a useful tool for preserving a sequence of data and further iterating over it.

Table of content:

  • Creating a List
  • Knowing the size of List
  • Adding Elements to a List:
    • Using append() method
    • Using insert() method
    • Using extend() method
  • Accessing elements from the List
  • Removing Elements from the List:
    • Using remove() method
    • Using pop() method
  • Slicing of a List
  • List Comprehension
  • Operations on List
  • List Methods

Python List

In this tutorial, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.

Python Lists

Lists are one of the four built-in data structures in Python. Other data structures that you might know are tuples, dictionaries, and sets. A list in Python is different from, for example, int or bool, in the sense that it's a compound data type: you can group values in lists. These values don't need to be of the same type: they can be a combination of boolean, String, integer, float values.

List literals are a collection of data surrounded by brackets, and the elements are separated by a comma. The list is capable of holding various data types inside it, unlike arrays.

For example, let's say you want to build a list of courses then you could have:

courses = ['statistics', 'python', 'linear algebra']

Note that lists are ordered collections of items or objects. This makes lists in Python "sequence types", as they behave like a sequence. This means that they can be iterated using for loops. Other examples of sequences are Strings, tuples, or sets.

Lists are similar in spirit to strings you can use the len() function and square brackets [ ] to access the data, with the first element indexed at 0.

Tip: if you'd like to know more, test, or practice your knowledge of Python lists, you can do so by going through the most common questions on Python lists here.

Now, on a practical note: you build up a list with two square brackets (start bracket and end bracket). Inside these brackets, you'll use commas to separate your values. You can then assign your list to a variable. The values that you put in a Python list can be of any data type, even lists!

Take a look at the following example of a list:

# Assign integer values to `a` and `b` a = 4 b = 9 # Create a list with the variables `a` and `b` count_list = [1,2,3,a,5,6,7,8,b,10] count_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note that the values of a and b have been updated in the list count_list.

Creation of List

A list is created by placing all the items inside a square bracket [] separated by commas. It can have an infinite number of elements having various data types like string, integer, float, etc.

list1 = [1,2,3,4,5,6] #with same data type list1 [1, 2, 3, 4, 5, 6] list2 = [1, 'Aditya', 2.5] #with mixed data type list2 [1, 'Aditya', 2.5]

Accessing Elements from a List

Elements from the list can be accessed in various ways:

  • Index based: You can use the index operator to access the element from a list. In python, the indexing starts from 0 and ends at n-1, where n is the number of elements in the list.

    Indexing can be further categorized into positive and negative indexing.

index = [1,2,3,4,5] index[0], index[4] #positive indexing (1, 5) index[5] #this will give an error since there is no element at index 5 in the list --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in ----> 1 index[5] #this will give an error since there is no element at index 5 in the list IndexError: list index out of range index[-1], index[-2] #negative indexing, here -1 means select first element from the last (5, 4)
  • Slice based: This is helpful when you want to access a sequence/range of elements from the list. The semicolon (:) is used as a slice operator to access the elements.
index[:3], index[2:] ([1, 2, 3], [3, 4, 5])

List Methods

Some of the most commonly used list methods are :

Background

Lists are an increasingly popular topic for those who are starting to learn Python as well as for those who are already experienced with the language. If we believe the search results in Google Trends, the search interest in this topic has been rising each and every year.

If you are a regular visitor to forums to answer or ask questions about Python programming, such as Stack Overflow, Quora or Reddit, you might know the reason behind it.

A lot of Python questions find their ways to these forums and continue to persist there, where users mark some as ‘duplicate’ or ‘sticky’, upvote them or discuss the right solution with others.

With this blog post, DataCamp wants to help you to tackle one topic, namely, the most frequently asked questions about lists in Python, and this in an interactive way!

Video liên quan

Postingan terbaru

LIHAT SEMUA