<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: UdayKiran137</title>
    <description>The latest articles on DEV Community by UdayKiran137 (@udaykiran137).</description>
    <link>https://dev.to/udaykiran137</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F521657%2F1329b3a7-fbc3-4e9d-939b-4e73c4ea1b4e.png</url>
      <title>DEV Community: UdayKiran137</title>
      <link>https://dev.to/udaykiran137</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/udaykiran137"/>
    <language>en</language>
    <item>
      <title>Understanding Java Memory Allocation: Stack vs. Heap</title>
      <dc:creator>UdayKiran137</dc:creator>
      <pubDate>Tue, 05 Oct 2021 03:28:12 +0000</pubDate>
      <link>https://dev.to/udaykiran137/understanding-java-memory-allocation-stack-vs-heap-1fm6</link>
      <guid>https://dev.to/udaykiran137/understanding-java-memory-allocation-stack-vs-heap-1fm6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Java Stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Java stack is a portion of your Computer's memory that stores temporary variables created by all functions you run. It's used to run a thread and can include both short-term values and references to other objects. It employs a &lt;strong&gt;LIFO&lt;/strong&gt; (last in, first out) data structure.&lt;/p&gt;

&lt;p&gt;When a method is called, a new block in the stack is created for that method. All local values and pointers to other objects utilized by the process will be stored in the new block. When the procedure is finished, the new block is erased and made available for the following method. The things you'll discover here are solely helpful for that function and won't last beyond it.&lt;/p&gt;

&lt;p&gt;This makes keeping track of the stack a breeze, as the most recently reserved block is also the first to be freed. The variables created for the method are saved in memory and can be accessed quickly.&lt;/p&gt;

&lt;p&gt;Because all variables created on the stack are wiped forever when a method ends, the memory size of a Java stack is typically significantly smaller than that of a Java heap area.&lt;/p&gt;

&lt;p&gt;An example of how to make a stack object is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void somefunction( )
{
    /* create an object "m" of class Member
    this will be put on the stack since the
    "new" keyword is not used, and we are
    creating the object inside a function
    */

    Member m;

} //the object "m" is destroyed once the function ends
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Java Heap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The heap is a location where Java objects are stored. It is formed when the program is executed, and its size may change as the program executes. It's easy for it to fill up, and when it does, garbage collection is started. Objects that are no longer in use are eliminated in order to make room for new ones.&lt;/p&gt;

&lt;p&gt;Unlike a &lt;strong&gt;Java stack&lt;/strong&gt;, where memory is allocated when the program is compiled, a &lt;strong&gt;Heap allocates Memory&lt;/strong&gt; while the program is run. When opposed to the direct and fast access of a stack, accessing variables stored here is a little slower.&lt;/p&gt;

&lt;p&gt;A global memory pool is analogous to a Heap. If you need the data or variables to live longer than the method or function in question, the heap will be used for Memory Allocation. All of the functionalities are accessible through the items found here.&lt;/p&gt;

&lt;p&gt;In addition, there is no set order in which blocks in a heap should be reserved. You can allocate blocks at any time and then release them as needed. As you might expect, keeping track of the sections that are free and can be allocated is much more difficult, but it can be divided into two generations or sub-areas.&lt;/p&gt;

&lt;p&gt;The young space (or nursery) and the old space are the two sub-areas. The memory allocation for new objects is usually done in the young space. Garbage collection occurs when the young space is filled. The youthful space is often used by short-term or transient things. When compared to a heap with no divisions, this aid speeds up garbage collection.&lt;/p&gt;

&lt;p&gt;An example of how to make an item on the Heap is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void somefunction( )
{
    /* create an object "m" of class Member
      this will be put on the heap since the 
      "new" keyword is used, and we are 
      creating the object inside a function
    */

    Member* m = new Member( );

    /* the object "m" must be deleted
      otherwise a memory leak occurs
    */

    delete m; 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Stack and Heap's Similarities and Dissimilarities&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Both of these methods are used by Java to allocate memory, and both are stored in RAM. To make things simpler, heap is utilized for dynamic memory allocation and stack is used for static memory allocation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Where is it kept? Variables allocated on the stack can be accessed directly from memory, allowing them to run very quickly. Objects on the heap, on the other hand, take longer to access.&lt;/p&gt;

&lt;p&gt;When will the allocation take place? When the application is compiled, memory is allocated on the stack. Meanwhile, when the program is started, it starts on the heap.&lt;/p&gt;

&lt;p&gt;And, since this is the case, if you wish to use the stack, you'll need to know exactly how much data and memory you'll need before compiling. The stack also has the problem of not being able to manage large blocks of variables that require a lot of memory. You should use heap if you don't know how much data you'll need at runtime or if you require memory for a large amount of data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In a Nutshell…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As methods and functions add and destroy local variables as needed, the stack's size will change.&lt;br&gt;
Without you having to control the memory allocation, memory is allocated and then freed.&lt;br&gt;
The size of a stack is limited, and this varies depending on the operating system.&lt;br&gt;
Variables stored on the stack remain valid for as long as the function that created them is active.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Heap:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Memory is not controlled automatically, and it is not handled as closely by the central processing unit as stack is. When these blocks are no longer required, you must clear the allotted RAM yourself.&lt;br&gt;
Memory leaks are common in heaps, where memory is allocated to useless objects and not available to other processes.&lt;br&gt;
The heap has no size restrictions.&lt;br&gt;
Objects on the heap are much slower to access than those in the stack. It's also slower to write to the heap's memory.&lt;/p&gt;

&lt;p&gt;Stack is more convenient and faster to use, but it has a number of drawbacks that you can overlook if you utilize heap.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When do you use "Stack"?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Only local variables that take up a small amount of memory can be stored on the stack. The best part is that memory management and allocation will not be a problem for you, and access to these objects will be lightning quick. It suffers from size restrictions and the inability to resize variables on the stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When do you use "Heap"?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If there are variables that need to be accessed worldwide rather than only by the procedures and functions that produced them, you use the heap to allocate memory. Because it has no memory size restriction, heap is also useful when you need a lot of memory. The variables in the heap can also be resized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally, I'd want to state that&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java allocates memory in two ways: stack and heap. When you have variables that require global access, heap is the way to go, whereas stack is the way to go for local variables that just require a limited amount of memory. When it comes to writing better Java applications, knowing when and how to use a stack and a heap is crucial.&lt;/p&gt;

&lt;p&gt;When dealing with memory leaks, it's also beneficial to understand how memory allocation works.&lt;/p&gt;

</description>
      <category>java</category>
      <category>stack</category>
      <category>heap</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Top 5 Operations for Python Lists</title>
      <dc:creator>UdayKiran137</dc:creator>
      <pubDate>Sun, 02 May 2021 10:11:17 +0000</pubDate>
      <link>https://dev.to/udaykiran137/top-5-operations-for-python-lists-38b8</link>
      <guid>https://dev.to/udaykiran137/top-5-operations-for-python-lists-38b8</guid>
      <description>&lt;p&gt;A list is an ordered collection of Python objects. As a mutable data type, lists are often used to store data that are subject to change at the runtime. Another advantageous feature of lists is that the allowance of having duplicate elements.&lt;br&gt;
Because of these features, lists are probably the most used data structure in any Python project. In this article, I will share the top 5 operations for the use of Python lists with you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Construction&lt;/strong&gt;&lt;br&gt;
The first apparent operation is how we create lists.&lt;/p&gt;

&lt;p&gt;I) The easiest way will be just listing composing elements inside a pair of square brackets. We use commas to separate these items. Optionally, we can use this declaration with the * operator if we want to repeat a particular list for specific times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; # Create an empty list
&amp;gt;&amp;gt;&amp;gt; empty_list = []
&amp;gt;&amp;gt;&amp;gt; # A list of numbers
&amp;gt;&amp;gt;&amp;gt; numbers = [1, 2, 3, 4, 5]
&amp;gt;&amp;gt;&amp;gt; # Use * operator
&amp;gt;&amp;gt;&amp;gt; triples = [1, 2, 3] * 3
&amp;gt;&amp;gt;&amp;gt; triples
[1, 2, 3, 1, 2, 3, 1, 2, 3] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;II) &lt;strong&gt;We can also create lists by slicing existing lists&lt;/strong&gt;. Some examples are shown below. One quick note, these slicing operations return lists as their return values, but just to make code snippets of the present article look clear, I don’t typically assign these lists to variables such that the Python interpreter will print them automatically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; # Suppose we have the following list to begin with
&amp;gt;&amp;gt;&amp;gt; list0 = [0, 2, 4, 6, 8, 10, 12, 14]
&amp;gt;&amp;gt;&amp;gt; # Create a list of the same elements
&amp;gt;&amp;gt;&amp;gt; list0[:] # This is equivalent to list0.copy()
[0, 2, 4, 6, 8, 10, 12, 14]
&amp;gt;&amp;gt;&amp;gt; # Specify the start, end, and step 
&amp;gt;&amp;gt;&amp;gt; list0[1:8:2]
[2, 6, 10, 14]
&amp;gt;&amp;gt;&amp;gt; # Create a list of the reverse order
&amp;gt;&amp;gt;&amp;gt; list0[::-1]
[14, 12, 10, 8, 6, 4, 2, 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;III) More generally, we can use the &lt;strong&gt;list()&lt;/strong&gt; function that creates lists from any iterable, such as tuples and dictionaries. When nothing is set, the list() function will create an empty list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; # from range
&amp;gt;&amp;gt;&amp;gt; list(range(4))
[0, 1, 2, 3]
&amp;gt;&amp;gt;&amp;gt; # from tuple
&amp;gt;&amp;gt;&amp;gt; list((5, 4, 3))
[5, 4, 3]
&amp;gt;&amp;gt;&amp;gt; # from dictionary
&amp;gt;&amp;gt;&amp;gt; list({'zero': 0, 'one': 1, 'two': 2})
['zero', 'one', 'two']
&amp;gt;&amp;gt;&amp;gt; # from map object
&amp;gt;&amp;gt;&amp;gt; list(map(len, ['one', 'hello', 'world!']))
[3, 5, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IV) Another convenient way is to use the list comprehension. The general format is &lt;strong&gt;[expression for ele in iterable if condition]&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; # Create a list of squares
&amp;gt;&amp;gt;&amp;gt; [x*x for x in range(5)]
[0, 1, 4, 9, 16]
&amp;gt;&amp;gt;&amp;gt; # Create a list of even numbers
&amp;gt;&amp;gt;&amp;gt; [x for x in range(10) if x%2 == 0]
[0, 2, 4, 6, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Access Elements&lt;/strong&gt;&lt;br&gt;
When we work with lists, we need to access individual elements or elements within a range.&lt;/p&gt;

&lt;p&gt;I) We can access &lt;strong&gt;individual elements&lt;/strong&gt; using &lt;strong&gt;indexing&lt;/strong&gt;. Typically, we use zero-based indexing that counts from the left. Alternatively, we can use reverse indexing, which counts from the right and begins with -1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers = [1, 4, 5, 7, 9, 13]
&amp;gt;&amp;gt;&amp;gt; # Forward Indexing
&amp;gt;&amp;gt;&amp;gt; numbers[2]
5
&amp;gt;&amp;gt;&amp;gt; # Reverse Indexing
&amp;gt;&amp;gt;&amp;gt; numbers[-1]
13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;II) We can access individual and a range of elements by unpacking. It’s pretty much like unpacking a tuple (i.e., a, b = (‘code’, 404)), and we can use * and _ to have a more powerful unpacking&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; pets = ['Dog', 'Cat', 'Turtle', 'Snake', 'Hamster']
&amp;gt;&amp;gt;&amp;gt; # when we're only interested in the first and last element
&amp;gt;&amp;gt;&amp;gt; a, *_, b = pets
&amp;gt;&amp;gt;&amp;gt; # elements as a list and two last elements
&amp;gt;&amp;gt;&amp;gt; *c, d, e = pets
&amp;gt;&amp;gt;&amp;gt; print(f'a: {a}, b: {b}, c: {c}, d: {d}, e: {e}')
a: Dog, b: Hamster, c: ['Dog', 'Cat', 'Turtle'], d: Snake, e: Hamster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;III) We can access multiple elements using slicing. It’s discussed in the above section when we use slicing to create new lists&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers = list(range(20))
&amp;gt;&amp;gt;&amp;gt; # Get even numbers
&amp;gt;&amp;gt;&amp;gt; numbers[::2]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
&amp;gt;&amp;gt;&amp;gt; # Get odd numbers
&amp;gt;&amp;gt;&amp;gt; numbers[1::2]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
&amp;gt;&amp;gt;&amp;gt; # Get some numbers in the middle
&amp;gt;&amp;gt;&amp;gt; numbers[5:9]
[5, 6, 7, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IV) We can access a random element in the list. It can be done using random.choice() function. Please note that this function is available in the random module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; import random
&amp;gt;&amp;gt;&amp;gt; lottery_tickets = ['A0049', 'A0050', 'A0051', 'A0052', 'A0053']
&amp;gt;&amp;gt;&amp;gt; # draw the winning ticket
&amp;gt;&amp;gt;&amp;gt; random.choice(lottery_tickets)
'A0050'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V) We can find the index of a particular object. To do that, we can use the index() method. Please note that we’ll encounter an error if the object isn’t on the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; students = ['John', 'Mike', 'Jennifer', 'Joe']
&amp;gt;&amp;gt;&amp;gt; # when an object exist
&amp;gt;&amp;gt;&amp;gt; students.index('Mike')
1
&amp;gt;&amp;gt;&amp;gt; # when an object doesn't exist
&amp;gt;&amp;gt;&amp;gt; students.index('Mary')
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
ValueError: 'Mary' is not in list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Conditionals&lt;/strong&gt;&lt;br&gt;
Certain operations rely on whether a particular condition is met or not. There are multiple possible conditions that pertain to lists in Python.&lt;/p&gt;

&lt;p&gt;I) We can check if a list is empty. When a list isn’t empty, it is evaluated as True. When it’s empty, using the not keyword will make the evaluation True. In other words, an empty list is evaluated as False.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; some_list = [1, 2]
&amp;gt;&amp;gt;&amp;gt; empty_list = []
&amp;gt;&amp;gt;&amp;gt; if some_list:
...     print('This is not an empty list')
... 
This is not an empty list
&amp;gt;&amp;gt;&amp;gt; if not empty_list:
...     print('This is to negate an empty list')
... 
This is to negate an empty list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;II) We can check if an element is in the list. To do that, we can use the item in the list expression. To negate it, we can use not in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; activities = ['soccer', 'tennis', 'swim', 'karate']
&amp;gt;&amp;gt;&amp;gt; # check if something is in the list
&amp;gt;&amp;gt;&amp;gt; 'tennis' in activities
True
&amp;gt;&amp;gt;&amp;gt; # check if something is not in the list
&amp;gt;&amp;gt;&amp;gt; 'swim' not in activities
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;III) We can check if the list contains all the elements of another list. We can take advantage of some built-in functionalities of the set datatype.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; larger_list = [7, 8, 9, 17, 18, 19]
&amp;gt;&amp;gt;&amp;gt; smaller_list = [8, 9, 18, 19]
&amp;gt;&amp;gt;&amp;gt; # use the issuperset function
&amp;gt;&amp;gt;&amp;gt; set(larger_list).issuperset(set(smaller_list))
True
&amp;gt;&amp;gt;&amp;gt; # use the issubset function
&amp;gt;&amp;gt;&amp;gt; set(smaller_list).issubset(set(larger_list))
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IV) We can check if the list contains any elements of another list. Similarly, as above, the set data type has built-in functionalities for this operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; large_list = [7, 8, 9, 17, 18, 19]
&amp;gt;&amp;gt;&amp;gt; small_list0 = [9, 28, 29]
&amp;gt;&amp;gt;&amp;gt; small_list1 = [28, 29]
&amp;gt;&amp;gt;&amp;gt; # use the intersection function
&amp;gt;&amp;gt;&amp;gt; bool(set(large_list).intersection(set(small_list0)))
True
&amp;gt;&amp;gt;&amp;gt; bool(set(large_list).intersection(set(small_list1)))
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Modify Elements&lt;/strong&gt;&lt;br&gt;
One major feature of lists is their mutability, which means we can modify their elements, such as adding and removing items.&lt;/p&gt;

&lt;p&gt;I) We can append an element at the end or insert one at a particular index using the append() and insert() methods, respectively&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; existing_numbers = [2, 4, 6, 8]
&amp;gt;&amp;gt;&amp;gt; # append an element at the end
&amp;gt;&amp;gt;&amp;gt; existing_numbers.append(10)
&amp;gt;&amp;gt;&amp;gt; existing_numbers
[2, 4, 6, 8, 10]
&amp;gt;&amp;gt;&amp;gt; # insert an element at an index
&amp;gt;&amp;gt;&amp;gt; existing_numbers.insert(0, 0)
&amp;gt;&amp;gt;&amp;gt; existing_numbers
[0, 2, 4, 6, 8, 10]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;II) We can extend the list by appending multiple elements using the extend() method, which takes in an iterable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers_to_extend = [1, 3, 5]
&amp;gt;&amp;gt;&amp;gt; # extend the list with another list
&amp;gt;&amp;gt;&amp;gt; numbers_to_extend.extend([7, 9])
&amp;gt;&amp;gt;&amp;gt; numbers_to_extend
[1, 3, 5, 7, 9]
&amp;gt;&amp;gt;&amp;gt; # extend the list with a tuple
&amp;gt;&amp;gt;&amp;gt; numbers_to_extend.extend((11, 13))
&amp;gt;&amp;gt;&amp;gt; numbers_to_extend
[1, 3, 5, 7, 9, 11, 13]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;III) We can replace certain elements directly. It can be achieved by assignment to the element at an index or elements within a range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers_to_replace = [1, 2, 3, 5]
&amp;gt;&amp;gt;&amp;gt; # replace an element
&amp;gt;&amp;gt;&amp;gt; numbers_to_replace[3] = 4
&amp;gt;&amp;gt;&amp;gt; numbers_to_replace
[1, 2, 3, 4]
&amp;gt;&amp;gt;&amp;gt; # replace a range of elements
&amp;gt;&amp;gt;&amp;gt; numbers_to_replace[1:3] = [0, 0]
&amp;gt;&amp;gt;&amp;gt; numbers_to_replace
[1, 0, 0, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IV) &lt;strong&gt;We can remove an element using the remove(), pop(), and del methods&lt;/strong&gt;. The remove() method removes the matching element at its first occurrence, the pop() method removes the element at the optionally specified index, and the default index is -1 (i.e., the last one), and the del statement removes an element with its index specified. Another thing to note is that the pop() method returns the value, while the other two return None.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove = [1, 2, 3, 4, 5, 6]
&amp;gt;&amp;gt;&amp;gt; # using the remove()
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove.remove(3)
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove
[1, 2, 4, 5, 6]
&amp;gt;&amp;gt;&amp;gt; # using the pop()
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove.pop(1)
2
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove
[1, 4, 5, 6]
&amp;gt;&amp;gt;&amp;gt; # using the del statement
&amp;gt;&amp;gt;&amp;gt; del numbers_to_remove[-1]
&amp;gt;&amp;gt;&amp;gt; numbers_to_remove
[1, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V) &lt;strong&gt;We can remove multiple elements using the clear() and del methods&lt;/strong&gt;. The clear() method removes all the elements of the list, and the del statement removes multiple elements with the range specified. Another handy way to remove all elements in the list is to assign the variable with an empty list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; # using clear
&amp;gt;&amp;gt;&amp;gt; numbers_to_clear = [3, 7, 8, 9, 10]
&amp;gt;&amp;gt;&amp;gt; numbers_to_clear.clear()
&amp;gt;&amp;gt;&amp;gt; numbers_to_clear
[]
&amp;gt;&amp;gt;&amp;gt; # using del
&amp;gt;&amp;gt;&amp;gt; numbers_to_del = [3, 7, 8, 9, 10]
&amp;gt;&amp;gt;&amp;gt; del numbers_to_del[2:]
&amp;gt;&amp;gt;&amp;gt; numbers_to_del
[3, 7]
&amp;gt;&amp;gt;&amp;gt; # using the re-assignment; note that this doesn't actually clear the old list
&amp;gt;&amp;gt;&amp;gt; numbers_to_reassign = [3, 4, 5]
&amp;gt;&amp;gt;&amp;gt; numbers_to_reassign = []
&amp;gt;&amp;gt;&amp;gt; numbers_to_reassign
[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VI) We can change the order of the list. Some common ways include sorting the list, reversing the list, and shuffling the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; numbers_to_order = [7, 9, -11, 4, 0, -13]
&amp;gt;&amp;gt;&amp;gt; # sort the list
&amp;gt;&amp;gt;&amp;gt; numbers_to_order.sort()
&amp;gt;&amp;gt;&amp;gt; numbers_to_order
[-13, -11, 0, 4, 7, 9]
&amp;gt;&amp;gt;&amp;gt; numbers_to_order.sort(key=lambda x: abs(x))
&amp;gt;&amp;gt;&amp;gt; numbers_to_order
[0, 4, 7, 9, -11, -13]
&amp;gt;&amp;gt;&amp;gt; # shuffle the list
&amp;gt;&amp;gt;&amp;gt; import random
&amp;gt;&amp;gt;&amp;gt; random.shuffle(numbers_to_order)
&amp;gt;&amp;gt;&amp;gt; numbers_to_order
[0, 9, -11, -13, 4, 7]
&amp;gt;&amp;gt;&amp;gt; # reverse the list
&amp;gt;&amp;gt;&amp;gt; numbers_to_order[::-1]
[7, 4, -13, -11, 9, 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Iteration&lt;/strong&gt;&lt;br&gt;
It’s a prevalent task that we need to perform certain operations by going through the list’s elements, which is termed iteration.&lt;/p&gt;

&lt;p&gt;I) We can iterate a list directly. Lists are iterables in Python, and thus we can iterate a list in a for loop directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; pets = ['Dog', 'Cat', 'Bird']
&amp;gt;&amp;gt;&amp;gt; for pet in pets:
...     print(pet)
... 
Dog
Cat
Bird
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;II) &lt;strong&gt;We can iterate a list using the enumerate() function&lt;/strong&gt;. This function allows us to track the count of the iteration. Optionally, we can set the start argument to specify the start number for the counting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; activities = ['soccer', 'karate', 'basketball']
&amp;gt;&amp;gt;&amp;gt; for i, activity in enumerate(activities, start=1):
...     print(f'Day {i}: {activity}')
... 
Day 1: soccer
Day 2: karate
Day 3: basketball
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;III) We can iterate a list using the reversed() function. This function allows us to iterate the list using the reverse order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
&amp;gt;&amp;gt;&amp;gt; arrivals = ['John', 'Mike', 'Jennifer']
&amp;gt;&amp;gt;&amp;gt; for student in reversed(arrivals):
...     print(student)
... 
Jennifer
Mike
John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusions for the 5 topics&lt;/strong&gt;&lt;br&gt;
This article summarizes the most common operations for Python lists. Here are some quick takeaways so that you can understand the topic in these 5 sentences&lt;br&gt;
Hope you like it &lt;/p&gt;

&lt;p&gt;1) We can construct lists by listing the items, using the list() function, and working with existing ones.&lt;br&gt;
2) We can access individual and multiple items using indexing and ranges.&lt;br&gt;
3) We can check whether the list has a particular item and its emptiness and relationship with other lists.&lt;br&gt;
4) We can append, insert, and remove one or more elements. Besides, we can change the order of lists.&lt;br&gt;
5) We can iterate lists using themselves and in combination with the enumerate() and reversed() functions.&lt;/p&gt;

</description>
      <category>python</category>
      <category>lists</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Stack</title>
      <dc:creator>UdayKiran137</dc:creator>
      <pubDate>Tue, 01 Dec 2020 10:12:26 +0000</pubDate>
      <link>https://dev.to/udaykiran137/stack-g6k</link>
      <guid>https://dev.to/udaykiran137/stack-g6k</guid>
      <description>&lt;p&gt;The &lt;strong&gt;stack&lt;/strong&gt; may be a linear arrangement that follows a specific order during which the operations are performed. The order of stack could also be LIFO (Last in First Out) or FILO (First In Last Out).&lt;br&gt;
A stack is generally an &lt;strong&gt;Abstract Data Type (ADT)&lt;/strong&gt;. This ADT is generally used in most programming languages it's an easy arrangement that permits adding and removing elements during a particular order. whenever a component is added, it goes on the highest of the stack, and therefore the only element which will be removed is that the element that's at the highest of the stack, a bit like a pile of objects.&lt;/p&gt;

&lt;p&gt;This is named the stack because this behaves like a real-world stack application the stack ADT will allow all the data operations at one end only at any time, we can use the top element of a stack&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj1bcjg4ij4j81w6yt5hk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj1bcjg4ij4j81w6yt5hk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generally, a stack is an abstract data type that is a set of elements, with two main principal operations:&lt;br&gt;
1)  Push (a)&lt;br&gt;
2)  Pop ()&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push&lt;/strong&gt;: which adds a component to the gathering, and&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pop&lt;/strong&gt;: which removes the foremost recently added element that wasn't yet removed.&lt;/p&gt;

&lt;p&gt;Not only push and pop but there are also other operations in a stack. They are :&lt;br&gt;
1)  IsEmpty ()&lt;br&gt;
2)  is full ()&lt;br&gt;
3)  Peek ()&lt;br&gt;
4)  Top ()&lt;br&gt;
5)  Search ()&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1vwzbwoz20ayklqd7dwy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1vwzbwoz20ayklqd7dwy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let us see the time complexity of all the operations in the stack:&lt;br&gt;
1) push (a) – 0(1)&lt;br&gt;
2) pop () – 0()1&lt;br&gt;
3) IsEmpty () – 0(1)&lt;br&gt;
4) ISFull () – 0(1)&lt;br&gt;
5) peek () – 0(1)&lt;br&gt;
6) top () – 0(1)&lt;br&gt;
7) search () – 0(n)&lt;/p&gt;

&lt;p&gt;We can generally write stacks in any of the major programming languages but I'm now writing stacks in ”python”&lt;br&gt;
Ok, let's see how we can implement stacks in python. Generally in Python stack can be implemented in 3 ways. they're implementation method using List &lt;br&gt;
Python’s built-in arrangement list is often used as a stack. Instead of push(), append() is employed to feature elements to the highest of the stack while pop() removes the element in LIFO order.&lt;br&gt;
Unfortunately, the list has a few shortcomings. The biggest issue is that it can run into speed issues because it grows. The items in the list are stored next to every other in memory, if the stack grows bigger than the block of memory that currently hold it, then Python needs to do some memory allocations. This can cause some append() calls taking for much longer than other ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features of stacks&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;•They are Dynamic data structures&lt;br&gt;
•They Do not have a fixed size and also &lt;br&gt;
•They Do not consume a fixed amount of memory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdb72caupua77i5igg8gv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdb72caupua77i5igg8gv.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uses of Stacks&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Basically, there are some particular uses of stacks in data structures. They are &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)Expression Handling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.Infix to suffix or Infix to Prefix Conversion −&lt;/p&gt;

&lt;p&gt;The stack is accustomed convert some infix expression into its suffix equivalent, or prefix equivalent. These suffixes or prefix notations are employed in computers to specific some expressions. These expressions aren't most acquainted with the infix expression, however, they need some nice blessings additionally. we tend to don't got to maintain operator ordering and parenthesis.&lt;/p&gt;

&lt;p&gt;2.Postfix or Prefix analysis analysis&lt;/p&gt;

&lt;p&gt;After changing into a prefix or ending notations, we've got to gauge the expression to urge the result. For that purpose, conjointly we want the assistance of stack organization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Backtracking Procedure&lt;/strong&gt; −&lt;/p&gt;

&lt;p&gt;Backtracking is one in every of the formula coming up with technique. For that purpose, we tend to dive away, if that method isn't economical, we tend to return to the previous state and go in another way. to induce back from the current state, we'd like to store the previous state. For that purpose, we'd like a stack. Some samples of backtracking are finding the answer for Knight Tour downside or N-Queen downside etc.&lt;/p&gt;

&lt;p&gt;3) Another nice use of stack is throughout the call and come back method. after we decided a operate from one alternative operate, that call statement might not be the primary statement. when career the operate, we tend to even have to come back from the operating space to the place, wherever we've left our management. thus we would like to resume our task, not restart. For that reason, we have a tendency to store the address of the program counter into the stack, then head to the operating body to execute it. when completion of the execution, it pops out the address from the stack and assign it to the program counter to resume the task once more.&lt;/p&gt;

&lt;p&gt;4) &lt;strong&gt;Syntax Parsing&lt;/strong&gt;-&lt;br&gt;
           Many compilers use a stack for parsing the syntax of expressions, program blocks, etc. before translating into low-level code.&lt;/p&gt;

&lt;p&gt;5) &lt;strong&gt;Parenthesis Checking&lt;/strong&gt; -&lt;br&gt;
            The stack is used to check the proper opening and closing of the parenthesis.&lt;/p&gt;

&lt;p&gt;6) &lt;strong&gt;String Reversal&lt;/strong&gt;&lt;br&gt;
            The stack is used to reverse a string. We push the characters of the string one by one into the stack and then pop character from the stack.&lt;/p&gt;

&lt;p&gt;Not only these but there are many uses of stacks like Function call, Memory Management, etc, etc &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# initial empty stack  
my_stack = []  
# append() function to push   
# element in the my_stack   
my_stack.append('k')  
my_stack.append('p')  
my_stack.append('s')  
print(my_stack)  
#pop() function to pop   
# element from my_stack in    
# LIFO order   
print('\nElements poped from my_stack:')  
print(my_stack.pop())  
print(my_stack.pop())  
print(my_stack.pop())     
print('\nmy_stack after elements are poped:')  
print(my_stack)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>python</category>
      <category>datastructure</category>
      <category>stacks</category>
      <category>queues</category>
    </item>
  </channel>
</rss>
