DEV Community

Rohan Kumar Bhoi
Rohan Kumar Bhoi

Posted on • Originally published at computertipstricks.tech

Lists and methods of the list in Python

What are the lists?

A list in Python is a data structure that stores an ordered collection of items, which can be of any data type, including numbers, strings, and even other lists.
A list can grow or shrink during the execution of the program. Hence it is also known as a dynamic array.
Because of the nature of lists they are commonly used for handling variable-length data.

Lists properties
Lists can be enclosed with square bracket '[ ]' and values inside the list are separated with commas

 fruits = ["apple", "banana", "cherry"]
 print(fruits)

 //Output
 ['apple', 'banana', 'cherry']
Enter fullscreen mode Exit fullscreen mode

Python Lists are mutable

Lists in Python are mutable, meaning the elements within a list can be changed after the list is created.

For example:

 fruits = ["apple", "banana", "cherry"]
 print("Before modification: ", fruits)

 fruits[1] = "orange"
 print("After modification: ", fruits)

 //Output
 Before modification:  ['apple', 'banana', 'cherry']
 After modification:  ['apple', 'orange', 'cherry']
Enter fullscreen mode Exit fullscreen mode

A list can contain dissimilar types, they are a collection of similar types.

For example:

 li = ["Sanjay", 30, 2.5]
 print(li)

 //Output
 ['Sanjay', 30, 2.5]
Enter fullscreen mode Exit fullscreen mode

Items in a list can be repeated, i.e., a list may contain duplicate items. Like printing, * can be used to repeat an element multiple times. An empty list is also feasible.

For example:

 ages = [5, 6, 5, 7, 6]        # duplicates allowed
 num = [10]*5                  # stores [10, 10, 10, 10, 10]
 lst = []                      # empty list, valid
 print(ages)
 print(num)
 print(lst)

 //Output
 [5, 6, 5, 7, 6]
 [10, 10, 10, 10, 10]
 []
Enter fullscreen mode Exit fullscreen mode

List slicing

List slicing in Python is a technique to extract a portion of a list, creating a new list that contains only a subset of the original list's elements. Slicing is performed by specifying the start index and the end index separated by a colon ':'.

The syntax for list slicing is as follows:
list[start:end]
Where 'start' is the index of the first element to include in the slice and 'end' is the index of the first element to exclude from the slice. The slice will include all elements from 'start' up to but not including 'end'.

Here are some examples of using list slicing in Python :

Getting a slice of a list:

fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
 print(fruits[1:4])   

 //Output
 ['banana', 'cherry', 'orange']
Getting a slice from the beginning of a list:
 fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
 print(fruits[:3])  

 //Output
 ['apple', 'banana', 'cherry']
Getting a slice until the end of a list:
 fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
 print(fruits[2:])   

 //Output
 ['cherry', 'orange', 'kiwi']
Getting a slice with a step:
 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 print(numbers[1::2])   

 //Output
 [2, 4, 6, 8, 10]
Using negative indexing in a slice:
 fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
 print(fruits[-3:-1])  

 //Output
 ['cherry', 'orange']
Enter fullscreen mode Exit fullscreen mode

These are just a few basic examples of using list slicing in Python. You can use slicing to extract any portion of a list based on the start and end indices and the step size, allowing you to work with subsets of data easily and efficiently.

List methods

Python lists have some built-in methods that you can use to perform several operations.

Click on the above Embeded link to see the Methods of List in Python...

Lists in Python | List Data Structure in Python

A list in Python is a data structure that stores an ordered collection of items, which can be of any data type, including numbers, strings, and even..

favicon computertipstricks.tech

Top comments (0)