List

Lists

- Lists are the data types.

- Lists are used to store multiple data in a single variable.

- Lists are created using square brackets [].

- Lists are changeable and allow duplicate values.

- We can change, add, or remove in a list.

- In the List, the first value has [0] 
index, and the second value has [1] index. (Index starts with 0)


Lists with different Data Types Values:



List with String, Integer, Boolean Values:



List Allow Duplicates Values:


List type():

- The type() method Returns the datatype of my_list. (In the below code)

Syntax: type(variable_name)



List len():

- len() stands for length.

- The len() method Returns the length of my_list. (In the below code)

Syntax: len(variable_name)



List Specific Single Value:

- In the List, the first value has [0] index, and the second value has [1] index. (Index starts with 0)



List Specific Multiple Values:

- In the List, the first value has [0] index, and the second value has [1] index. (Index starts with 0)

- In the below code, [0:2] means Returns the values between 0 and 2 indexes.

- [0:2], 0 is included and 2 is excluded that's why python returns only 0 and 1 index values.

[0:3]0 is included and 3 is excluded that's why python returns only 01, and 2 index values.



Change string in List:

- Syntax: list(a)

- a should be another datatype variable to change into a list.

- a is the string to change into a list. (In the below code)

- We can take any datatype to change into a list. Like- tuple(), set{}, dict{} etc.



Change Value in List:

- Syntax: my_list[1] = 19

- my_list is the variable, [1] is the value index (take that value index which is you want to change), and 19 is the new value. (In the below code)



List append():

- The append() method is used to add a new Value at the end of a list.

Syntax: variable_name.append("New Value")



List insert():

- The insert() method is used to insert a new value in a specific position in the list.

- Syntax: variable_name.insert(position, "New Value")



List remove():

- The remove() method is used to remove the specific value in a list.

Syntax: variable_name.remove(Value in list)



List pop():

- The pop() method is used to remove the ending value in a list.

Syntax: variable_name.pop()



List clear():

- The clear() method is used to clear the all values in a list.

- Output: [].

Syntax: variable_name.clear()



List del:

- del stands for delete.

- del used to delete a list and return nothing.

- del also use to delete the specific value in a list by their position.


Delete a List:

Syntax: del variable_name



Delete the specific values in a List by their position:

Syntax: del variable_name[1]

- 1 is the value index.



Negative indexing in List:

- Return the last value in a list.

- In list, index[-1] = Return the last value in a List and index[-2] = Return the second last value in a List .

Syntax: variable_name[-1]

Comments