Iterate through multiple lists at the same time [duplicate] - python

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 7 years ago.
Is it possible to iterate through multiple lists and return arguments from different lists within the same loop?
I.e., Instead of -
For x in trees:
Print(x)
For y in bushes:
Print(y)
Something like -
For x,y in trees,bushes:
Print(x +"\n"+ y)

You can simply use zip or itertools.izip:
for x, y in zip(trees, bushes):
print x, y

You can use zip():
a=['1','2','2']
b=['3','4','5']
for x,y in zip(a,b):
print(x,y)
output:
1 3
2 4
2 5

Related

How to condense 'text' in x in if statement? [duplicate]

This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have a code in which there is a lineif y in x or f in x or h in x and so on. Even if it works, it seems to be very long.
I tried if (y or f or h) in x and if [y,f,h]in x but nothing worked.
Can anyone help?
Use any along with a generator expression:
y = 'y'
f = 'f'
x = 'x'
print(any(char in 'abcdex' for char in [y, f, x]))
Output:
True

How can I compound a if and for loop inside a print statement in Python [duplicate]

This question already has answers here:
python 3 print generator
(2 answers)
Closed 4 years ago.
I am trying to combine this into one line -
for x in set(l_rooms):
if l_rooms.count(x) == 1:
print(x)
I tried:
print(x for x in set(l_rooms) if l_rooms.count(x)== 1)
but that only yields a generator object and doesn't actually print
print([x for x in set(l_rooms) if l_rooms.count(x)==1])

How do you display a list of numbers using a for loop and one int variable? [duplicate]

This question already has answers here:
How to loop backwards in python? [duplicate]
(5 answers)
Closed 5 years ago.
How do you create a display of a list of variables, stemming from one number, all while using a for loop?
Here is what I am thinking of:
Hours = 6
for x in Hours():
Print(x <= Hours)
# This is obviously wrong
The answer:
6
5
4
3
2
1
Use the range function with a step of -1
In your case: range(6,0,-1)
for i in range(6, 0, -1):
print(i)
Explication of the range function: https://www.pythoncentral.io/pythons-range-function-explained/
Use this one-liner:
print('\n'.join([str(i) for i in range(6, 0, -1)]))
This gets a list of integers from 6 to 0 (excluding zero), stepping backwards by one, takes a string of each int str(i) and joins these with newlines \n.
Alternative one line answer:
[print(x) for x in range(6, 0, -1)]

Selecting Randomly within an Array [duplicate]

This question already has answers here:
Randomly extract x items from a list using python
(3 answers)
Closed 8 years ago.
Given an array x = [2,3,5,4,1,7,4,2,8] I am looking to create a second array y which is a length p and consists of a random election of the elements within x. Is there an easier way other than doing the following
x = [2,3,5,4,1,7,4,2,8]
random.shuffle(x)
p = 5
y = x[0:p]
print y
Use random.sample:
x = [2,3,5,4,1,7,4,2,8]
y = random.sample(x, p)

How to output an index while iterating over an array in python [duplicate]

This question already has answers here:
Accessing the index in 'for' loops
(26 answers)
Closed 8 years ago.
I am iterating over an array in python:
for g in [ games[0:4] ]:
g.output()
Can I also initialise and increment an index in that for loop and pass it to g.output()?
such that g.output(2) results in:
Game 2 - ... stuff relating to the object `g` here.
Like this:
for index, g in enumerate(games[0:4]):
g.output(index)
Use the built-in enumerate method:
for i,a in enumerate(['cat', 'dog']):
print '%s is %d' % (a, i)
# output:
# cat is 0
# dog is 1

Categories