does a "for ... in" loop in python increase space complexity?

  • Last Update :
  • Techknowledgy :

No, the loop by itself does not. Consider:

for char in some_string:
   print(char)

Here's a version with list comprehension:

def findNumVowels(s):
   vowels = ['a', 'e', 'i', 'o', 'u']
return len([char_literal
   for char_literal in s
   if char_literal in vowels
])

findNumVowels("Kunal")

Suggestion : 2

No, the loop by itself does not. anycodings_python-3.x Consider:,As you can see Python strings are anycodings_python-3.x immutable which means they cannot be anycodings_python-3.x changed after they are created. So we anycodings_python-3.x are simply indexing the string with the anycodings_python-3.x for..in construct, which does not take anycodings_python-3.x any extra space complexity.,Would the for ... in loop add to the space anycodings_python-3.x complexity of this function by creating a anycodings_python-3.x new string for each char in s, or is this anycodings_python-3.x syntactic sugar that abstracts away the fact anycodings_python-3.x that we are accessing a specific index of a anycodings_python-3.x string?,It only requires one extra object of anycodings_python-3.x constant size. This is constant with anycodings_python-3.x respect to the size of the string. So, anycodings_python-3.x it doesn't matter if my string is 10 or anycodings_python-3.x 1000 characters long, it always requires anycodings_python-3.x one extra str to loop over it. anycodings_python-3.x Therefore, it takes constant space.

Say I had the following function:

def findNumVowels(s):
   vowels = ['a', 'e', 'i', 'o', 'u']
numVowels = 0
for char in s:
   if char in vowels:
   numVowels += 1
return numVowels

print(findNumVowels("hello world")) # 3

No, the loop by itself does not. anycodings_python-3.x Consider:

for char in some_string:
   print(char)

Here's a version with list anycodings_python-3.x comprehension:

def findNumVowels(s):
   vowels = ['a', 'e', 'i', 'o', 'u']
return len([char_literal
   for char_literal in s
   if char_literal in vowels
])

findNumVowels("Kunal")

Suggestion : 3

Last Updated : 12 Feb, 2021,GATE CS 2021 Syllabus

Output:
 

1
2
3
4
5
  • Output: 
     
1 3 5
  • Output:
     
1 3 5

Suggestion : 4

Space Complexity − Memory usage of a data structure operation should be as little as possible.,Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.,These notations are named as how they use operator in expression. We shall learn the same here in this chapter.,The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are −

If you are using Linux or UNIX, then check whether GCC is installed on your system by entering the following command from the command line −

$ gcc - v

If you have GNU compiler installed on your machine, then it should print a message such as the following −

Using built - in specs.
Target: i386 - redhat - linux
Configured with: .. / configure--prefix = /usr .......
Thread model: posix
gcc version 4.1 .2 20080704(Red Hat 4.1 .2 - 46)

Problem − Design an algorithm to add two numbers and display the result.

Step 1− START
Step 2− declare three integers a, b & c
Step 3− define values of a & b
Step 4− add values of a & b
Step 5− store output of step 4 to c
Step 6− print c
Step 7− STOP

Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the variable part of the algorithm, which depends on instance characteristic I. Following is a simple example that tries to explain the concept −

Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C← A + B + 10
Step 3 - Stop

For example, for a function f(n)

Ο(f(n)) = {
   g(n): there exists c > 0 and n0 such that f(n)≤ c.g(n) for all n > n0.
}