Language Python Tech Website

Lists In Python. Difference between List & Strings.

List is a useful data type in Python. It helps us store and work with our number of values. The values in a list are known as elements. These elements may belong to a different data types. The elements in a list are enclosed in square brackets under separated using a comma.

Accessing Elements In A List

The elements of a list are accessed in the same way as characters are accessed in the string. Every element in a list has an index number. The index of the first element (from left) in the list is zero.

If the length of the list is n, then the index of last element in the list a always (n-1).

Unlike strings, lists are mutable i.e. we can change the elements in the list.

Difference Between List And Strings

Both list and strings belong to the sequence type in Python. But there are some differences between the two. Below given the differences between the list and strings :- 

1. List are mutable where as strings are immutable.

 and the other differences is 

2. List is a collection of elements that may be of different data types but strings is a  collection of characters.

List operations

Python provides many basic operations which can be performed on lists. Some of the operations are as follows :-

1. Concatenation

Concatenation operation is used to join two lists. The operator used to perform concatenation is denoted by ‘+’ symbol.

2. Repetition

Repetition operation is to repeat the elements in a list for a specified number of times. The operator used for repetition is denoted by the ‘*’ symbol.

3. Membership

Membership operation is used to check whether a value is present in a list or not. The membership operator ‘in’ returns True if the element is present in the list and return false otherwise.

4. Slicing

Like strings, slicing operation also works with lists. Slicing operation is used to extract a part of a list. In slicing, we specify the start and the end index numbers with the step value to specify the elements to extracted.

start : Starting index from where the slicing of list starts.

stop : Ending index where the slicing of list stops. The element at the stop index is not included in the result.

step : it specifies the value by which the index is incremented. The default step value is 1.

Returns the number of elements in the list.

Returns the maximum value in the list.

Returns the minimum value in the list.

Returns the sum of the all values in the list.

Adds the element passed as an argument at the end of the list.

Accepts a list as an argument and ads all elements of the list to the end of the given list.

Inserts and element at a particular index in the list. The first argument is the index and the second argument is the element to be inserted in the list.

Returns the number of the times the value given is argument appears in the list.

Removes the value given as argument from the list if the element is present multiple times only the first appearance mode if the element is not present then value is generated.

Arranges the elements of the list in ascending order. If we reverse=True as an argument, the elements of the list arrangement descending order.

C# is a popular programming language. It was developed by a Microsoft team led by Anders Hejlsberg.

Python has extensive collection of libraries. One such useful library is matplotlib. This library helps us plot graphs and charts to analyse data in a better manner.

About the author


Add Comment

Click here to post a comment