printing from list of tuples without brackets and comma - python-3.x

From this list of tuples:
lst = [('abc',1), ...]
I want to print out
abc 1
I have tried
var = lst[0]
print (var[0], var[1])
Is there a direct way to print the desired result?
Another way is using for loop but I think it is completely useless clutter for this.

I have many answers!
If you only want to print the first one, you can do:
lst = [('abc',1),('efg',99)]
print(*lst[0]) # Output: abc 1
If you want to do that with each element of the list:
lst = [('abc',1),('efg',99)]
for e in lst:
print(*e)
Finally, if you want to print this tuple but still save it in a variable, you can do:
lst = [('abc',1),('efg',99)]
print(*(var:=lst[0])) # Output: abc 1
print(var) # Output: ('abc', 1)
print(*var) # Output: abc 1

Related

Reverse the given string

I'm writing a program to print the reverse of a given string. I am able to write the function to reverse the string but not able to take as many inputs as given in the test cases.
I've tried using "while" loop but I'm not able to get all the test cases as input. Maybe the syntax is wrong. I'm a newbie.
def rev(sample_string):
return sample_string[::-1]
t_case = int(input()) #No. of testcases
i = 0
while i < t_case:
sample_string = str(input("")) #take as many inputs as no. of
#testcases
i =+ 1
print(rev(sample_string))
for sample input: 2, ab, aba --------
output should be: ba, aba //(in separate lines)
If you want to save and print multiple strings, you need a datatype to do so.
List would do that job:
def rev(sample_string):
return sample_string[::-1]
t_case = int(input()) #No. of testcases
i = 0
string_list = [] # List to store the strings
while i < t_case:
string_list.append(str(input(""))) #take as many inputs as no. of
#testcases
i += 1
for string in string_list: # iterate over the list and print the reverse strings
print(rev(string))

Concatenate list and tuple output not favourable

I want to create an output that concatenates the list and tuple to give a single output
def conca(names,storey):
for name in names:
i = 0
d = "%s has %d"%(name,storey[i])
print(d)
i = i+1
conca(names=("White house","Burj khalifa",
"Shit"),storey = [3,278,45])
But it gives an output like
White house has 3
Burj khalifa has 3
Shit has 3
But i dont want only 3. I want i to increase. Give output like
White house has 3
Burj khalifa has 278
Shit has 45
Why is i not incrementing. What am i doing wrong
Problem:
You define i inside loop, so you reset to 0 in every iteration resulting in adding first storey every time.
Corrected:
def conca(names, storey):
i = 0
for name in names:
d = "%s has %d"%(name,storey[i])
print(d)
i = i+1
conca(names=("White house","Burj khalifa",
"Shit"), storey=[3,278,45])
You can also use zip() to iterate over lists simultaneously:
def conca(names, storey):
for name, st in zip(names, storey):
print(f'{name} has {st}')
conca(names=("White house","Burj khalifa",
"Shit"), storey=[3,278,45])

Common values in a Python dictionary

I'm trying to write a code that will return common values from a dictionary based on a list of words.
Example:
inp = ['here','now']
dict = {'here':{1,2,3}, 'now':{2,3}, 'stop':{1, 3}}
for val in inp.intersection(D):
lst = D[val]
print(sorted(lst))
output: [2, 3]
The input inp may contain any one or all of the above words, and I want to know what values they have in common. I just cannot seem to figure out how to do that. Please, any help would be appreciated.
The easiest way to do this is to just count them all, and then make a dict of the values that are equal to the number of sets you intersected.
To accomplish the first part, we do something like this:
answer = {}
for word in inp:
for itm in word:
if itm in answer:
answer[itm] += 1
else:
answer[itm] = 1
To accomplish the second part, we just have to iterate over answer and build an array like so:
answerArr = []
for i in answer:
if (answer[i] == len(inp)):
answerArr.append(i)
i'm not certain that i understood your question perfectly but i think this is what you meant albeit in a very simple way:
inp = ['here','now']
dict = {'here':{1,2,3}, 'now':{2,3}, 'stop':{1, 3}}
output = []
for item in inp:
output.append(dict[item])
for item in output:
occurances = output.count(item)
if occurances <= 1:
output.remove(item)
print(output)
This should output the items from the dict which occurs in more than one input. If you want it to be common for all of the inputs just change the <= 1 to be the number of inputs given.

Trying to print a list in reverse?

I am trying to print this list in descending order however this is not working any ideas why?
l = ["Germany",3,2,10,"Italy",7,9,1,"canada",4,5,3,"china",4,3,9]
d_list = []
for i in range(len(l)-2, -1, -3):
d_list.append(l[i-1])
d_list.append(l[i])
d_list.append(l[i+1])
print (d_list)
print [i for i in reversed(l)]
because reversed returns an iterator, so it wouldn't be enough to just print reversed.
Or what does descended order mean here for you?
Another option would be to reverse the entire list, then print it.
l[::-1]
for x in range(len(l)):
print(l[x-1])

Function that prints each element of a list and its index per line

I am trying to write a function that can print the element and index of a list. I want to do this without using the enumerate built in function and do it using for loops.
I was able to print out the element but I couldn't figure out a way to loop the index of my list.
Is there any good way I could work around this? Many thanks.
You could do this, simply iterating over the range of numbers regarding the length of your list:
def item_and_index(my_list):
for i in range(len(my_list)):
print(my_list[i], i)
This is exactly what you need, a function using for loops and not the enumerate function.
>>> L = ['a', 'b', 'c']
>>> for i in range(len(L)):
... print(i, L[i])
...
0 a
1 b
2 c
You could also try this:
i = 0
for elem in L:
print(i, elem)
i += 1

Resources