Print list string c#
Python ListsLists 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. Show
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.
Creating a ListLists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesnt need a built-in function for the creation of a list. Note Unlike Sets, the list may contain mutable elements. Python3
Output:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items
Geeks
Geeks
Multi-Dimensional List:
[['Geeks', 'For'], ['Geeks']]
Creating a list with multiple distinct or duplicate elementsA list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation. Python3
Output:
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
List with the use of Mixed Values:
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
Knowing the size of ListPython3
Output:
0
3
Adding Elements to a ListUsing append() methodElements can be added to the List by using the built-in append() function. Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used. Tuples can also be added to the list with the use of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append() method. Python3
Output: Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, (5, 6)] List after Addition of a List: [1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']] Using insert() methodappend() method only works for the addition of elements at the end of the List, for the addition of elements at the desired position, insert() method is used. Unlike append() which takes only one argument, the insert() method requires two arguments(position, value). Python3
Output:
Initial List:
[1, 2, 3, 4]
List after performing Insert Operation:
['Geeks', 1, 2, 3, 12, 4]
Using extend() methodOther than append() and insert() methods, theres one more method for the Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list. Note append() and extend() methods can only add elements at the end. Python3
Output:
Initial List:
[1, 2, 3, 4]
List after performing Extend Operation:
[1, 2, 3, 4, 8, 'Geeks', 'Always']
Accessing elements from the ListIn order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list. The index must be an integer. Nested lists are accessed using nested indexing. Python3
Output:
Accessing a element from the list
Geeks
Geeks
Accessing a element from a Multi-Dimensional list
For
Geeks
Negative indexingIn Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc. Python3
Output:
Accessing element using negative indexing
Geeks
For
Removing Elements from the ListUsing remove() methodElements can be removed from the List by using the built-in remove() function but an Error arises if the element doesnt exist in the list. Remove() method only removes one element at a time, to remove a range of elements, the iterator is used. The remove() method removes the specified item. Note Remove method in List will only remove the first occurrence of the searched element. Python3
Output:
Initial List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
List after Removal of two elements:
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
List after Removing a range of elements:
[7, 8, 9, 10, 11, 12]
Using pop() methodPop() function can also be used to remove and return an element from the list, but by default it removes only the last element of the list, to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method. Python3
Output:
List after popping an element:
[1, 2, 3, 4]
List after popping a specific element:
[1, 2, 4]
Slicing of a ListIn Python List, there are multiple ways to print the whole List with all the elements, but to print a specific range of elements from the list, we use the Slice operation. Slice operation is performed on Lists with the use of a colon(:). To print elements from beginning to a range use [: Index], to print elements from end-use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print the whole List with the use of slicing operation, use [:]. Further, to print the whole List in reverse order, use [::-1]. Note To print elements of List from rear-end, use Negative Indexes. Python3
Output:
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Slicing elements in a range 3-8:
['K', 'S', 'F', 'O', 'R']
Elements sliced from 5th element till the end:
['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Printing all elements using slice operation:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Negative index List slicingPython3
Output:
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Elements sliced till 6th element from last:
['G', 'E', 'E', 'K', 'S', 'F', 'O']
Elements sliced from index -6 to -1
['R', 'G', 'E', 'E', 'K']
Printing List in reverse:
['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']
List ComprehensionList comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. Syntax:
Example: Python3
Output: [1, 9, 25, 49, 81]For better understanding, the above code is similar to Python3
Output: [1, 9, 25, 49, 81]Refer to the below articles to get detailed information about List Comprehension.
Operations on List
List Methods
Built-in functions with List
Recent articles on Lists
More on Python List
Useful Links:
Article Tags :
Python
python-list Practice Tags :
python-list
Read Full Article
|