Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,945 questions

51,887 answers

573 users

How to use one line for loop in Python

6 Answers

0 votes
lst = ['python', 'c', 'c++', 'c#', 'java']

for s in lst: print(s)




     
     
'''
run:
     
python
c
c++
c#
java
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
dict = {'python': 5, 'php': 3, 'java': 9, 'c++': 1, 'c':8}

for key, val in dict.items(): print(key, val)




     
     
'''
run:
     
python 5
php 3
java 9
c++ 1
c 8
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
for i in range(1, 10): print(i)



     
     
'''
run:
     
1
2
3
4
5
6
7
8
9
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
lst = [1, 2, 3, 4, 5]

lst = [x**2 for x in lst]

print(lst)



     
     
'''
run:
     
[1, 4, 9, 16, 25]
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
lst = [1, 2, 3, 4, 5, 6, 7]

lst = [x for x in lst if x%2 == 0]

print(lst)



     
     
'''
run:
     
[2, 4, 6]
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
lst = [1, 2, 3, 4, 5, 6, 7]

lst = [x + 10 if x % 2 == 0 else x for x in lst]

print(lst)



     
     
'''
run:
     
[1, 12, 3, 14, 5, 16, 7]
  
'''

 



answered Apr 23, 2021 by avibootz

Related questions

1 answer 158 views
1 answer 156 views
1 answer 172 views
1 answer 195 views
1 answer 189 views
1 answer 140 views
...