Python 3: Concept of LIST in Details

Python 3: Concept of LIST in Details

hackernoon.com hackernoon.com1 week ago in #Dev Love66

@yogesh_ahdemYogesh Raghupati Learning from Mistakes because you Learn Programming by making ERRORS. Let’s Learn-Together♥ The dictionary meaning of LIST is a number of connected items or names written or printed consecutively, typically one below the other. In real world, lists are considered in many ways like playlist of songs, collection of your favourite videos, a list of user database and there are many more. But, if you are given a task about handling a list of users and filter out specific users under specific conditions then it becomes a tedious job to filter the list by going through each and every user. In such scenarios, programming comes into place to make your work easier. So, now let us understand ‘What is a LIST?’ in python programming. In python, the list is a mutable, or changeable, ordered sequence of elements where each element or value that is inside of a list is called an item. In string, how each value/element is represented as a character similarly in list, the each value/element is separated by comma where each element can be a string, variable or constants enclosed in [] bracket. Syntax of List: [‘LetsUpgrade’, ‘Batch1’, B2, B3] Nested List: [‘LetsUpgrade’, [‘java’, ‘python’, ‘c ‘], 1, 2, [‘B1’, ‘B2’], ‘Exam’] Topics to be covered in LIST are as follows: Indexing Lists Lists are Mutable Updating and Slicing Lists Use of operators in List Methods used in List Built-in functions in List Deleting Elements from List Constructing a List with List Items List and Strings Parsing lines Objects and values Aliasing List arguments List Comprehensions Using Conditionals with List Comprehensions Nested Loops in a List Comprehension Debugging We are going understand the above topics through Examples not by theoretical… #Indexing Lists: Each item in a list corresponds to an index number, which is an integer value, starting with the index number 0. For the list COURSES, the index breakdown looks like this: The first item, the string ‘Java’ starts at index 0, and the list ends at index 4 with the item ‘Javascript’. print(COURSES[3]) OUTPUT: R If we call the list COURSES with an index number of any that is greater than 4, it will be out of range as it will not be valid: print(COURSES[5]) Output: IndexError: list index out of range The above one was Positive Indexing and now let’s see Negative Indexing as shown below: For the same list COURSES, the negative index breakdown looks like this print(COURSES[-3]) 1 2 Output Python #Lists are Mutable: Example:- numbers = [17, 123] numbers[1] = 5 print(numbers) [17, 5] Therefore, as per above example lists are changeable i.e mutable. #Updating and Slicing Lists: Updating We can use indexing to change items within the list, by setting an index number equal to a different value. This gives us greater control over lists as we are able to modify and update the items that they contain. If we want to change the string value of the item at index 1 from ‘C ‘  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply