Receive items from an ordered list in a variable - Python

Asked

Viewed 66 times

-1

Good afternoon, you guys!

I started studying Python recently, and I have a question that may seem basic but it’s complicating me.

I wanted to receive the items of a list in an ordered form variable.

For example:

I have a list:

num = [2,5,7,9]

How do I receive these items in a variable, type:

var = num
print(var)

And the output is 2579, not [2, 5, 7, 9] as it is.

  • I didn’t quite understand... I created an example and it worked like you expect, take a look: https://repl.it/repls/ReflectingHummingTriangle

  • Yes, it receives the list in the VAR variable and prints it. I wanted to receive only the items. For then, when printing, instead of printing [2,5,7,9], which is what he gets, print only her items out of a list, like 2579.

  • 1
  • It doesn’t answer exactly, but it helped me understand that there are other possibilities, like turning into a tuple. Thank you very much, man!

2 answers

3

TL;DR

nums = [2, 5, 7, 9]

valor = "".join(nums)
print(valor)  # "2579"

Motive

By checking the job documentation print you will note the following sentence:

All non-keyword Arguments are converted to strings like str() does and Written to the stream...

Free translation:

All unnamed arguments are converted to strings like str() and then written in stream...

That is to say, print(lista) has the same result as print(str(lista))* because the list is converted to string. You can check that str([1, 2, 3]) == '[1, 2, 3]', that is, a string containing the list elements separated by ", " and wrapped in clasps.

* In theory they should always be the same results, but I did not perform tests with all native types and custom classes.

Therefore, to customize the way you want to show your list, just access it element by element or using specific methods.

Using str.join()

The method str.join() binds the elements of a string iterable and concatenates with a separator.

Since your list is integer, you will need to convert the values to string before using the method str.join(). You can do it using map (string = map(str, nums)) or list comprehensions (string = [str(n) for n in nums]).

Ex.:

nums = [2, 5, 7, 9]

valor = "".join(str(n) for n in nums)
print(valor)  # "2579"

demo_2 = "-".join(map(str, nums))
print(demo_2)  # "2-5-7-9"

demo_3 = ", ".join(map(str, nums))
print(demo_3)  # "2, 5, 7, 9"

Using print()

You can print element by element without spaces between them using the parameter end of function print():

nums = [2, 5, 7, 9]

for n in nums:
    print(n, end="")  # "2579"

for n in nums:
    print(n, end="-")  # "2-5-7-9"

for n in nums:
    print(n, end=", ")  # "2, 5, 7, 9"
  • I am formulating a better explanation, but for now I leave the above solutions.

  • Wow, that was a great explanation, thank you very much! Now I know you have how to convert the items into a string. I tried to turn it into a string with: num = [2, 5, 7, 9] var = "". Join(num) print(var) But it returns me "Typeerror: Sequence item 0: expected str instance, int found" Is it something of syntax that I might have missed?

  • Rectifying: It worked, buddy. It was just the lack of some attributes. I did it like this: value = "". Join(map(str,nums)) print(value) !

  • It was my fault, the list numbers should be converted. Your solution with map works

0

Another way to get the result desired by the questioner is by using the combination of the function map() with the expression lambda. In this way the following code can be implemented:

num = [2, 5, 7, 9]

r = ''.join(map(lambda i: str(i), num))

print(r)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.