5

Consider a list I want to parse using a for :

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
   print i

will return :

"Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"

However, if I want to put it to a (str) variable, like :

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
    var=i

first I have to declare another var variable, which is silly but whatever. then,

print var

will return the last element of the list, which is "Paris", because the variable is overwritten for each iteration right.

So my question is : how can I assign the output of my loop "i", for each iteration, to a variable in Python ?

Sorry for the sillyness of this question but this is a concept I can't seem to figure out clearly.

Nahid O.
  • 171
  • 1
  • 3
  • 14
  • 1
    why cant u try with a dictionary? – SuperNova Jun 20 '16 at 08:23
  • 3
    The answer is probably quite trivial, but I fail to understand what exactly you're asking for. Could you elaborate? What result do you expect? Note that the first example will **not** result in the output you claim... – skyking Jun 20 '16 at 08:25
  • And what would be the names of the variables? – thefourtheye Jun 20 '16 at 08:25
  • In terms of result : do you see the result of `print i` ? I would simply have the result of this print to a variable. – Nahid O. Jun 20 '16 at 08:28
  • 3
    What do you mean *"to a variable"*? You already have a list, which is assigned to a variable. What variables would you like the items to be assigned to, *and why?* – jonrsharpe Jun 20 '16 at 08:31
  • 1
    To be completely correct, *printing* and *returning* are two distinct concepts. Your loop doesn't *return* anything. – tripleee Jun 20 '16 at 08:36
  • 2
    You already have each element in the list stored as a variable: the original list itself. – TigerhawkT3 Jun 20 '16 at 08:37
  • I think my question is unclear, so I will re-formulate my question in an other post. Thanks, – Nahid O. Jun 20 '16 at 13:13

7 Answers7

2

If I understand well, you'd like to dynamically create variables. Here it is.

from collections import OrderedDict

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
d = OrderedDict()
for idx, value in enumerate(friends):
    key = 'var' + str(idx)
    d[key] = value 

print(d)
# Output
OrderedDict([('var0', 'Joe'), ('var1', 'Zoe'), ('var2', 'Brad'), ('var3', 'Angelina'), ('var4', 'Zuki'), ('var5', 'Thandi'), ('var6', 'Paris')])
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
  • wouldn't list(enumerate(friends)) be easier/clearer here? – PdevG Jun 20 '16 at 08:35
  • @Skirrebattie, I don't think so. `for idx, v in enumerate(l)` is widely used. – SparkAndShine Jun 20 '16 at 08:40
  • Look at your `OrderedDict`. Now look at `friends`. Note how you have used several lines of codes to reinvent the `list` type. The OP's question is fundamentally flawed and any answers cannot possibly be reasonable. – TigerhawkT3 Jun 20 '16 at 08:42
2

I also have this question, this is how I managed to solve it somewhat:

    friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]

    new_friends = ' '.join([x for x in friends])

    print(new_friends)

Will return:

    Joe Zoe Brad Angelina Zuki Thandi Paris
e71az
  • 21
  • 4
1
var = ''
friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
    var=i 

if list and loop are in function then declare var as global

global var

in starting of function

hb X
  • 69
  • 5
  • This is wrong, as the original question doesn't want `var` to have the last assignment, but all values. – Ricardo Jun 21 '23 at 17:20
1

I think the easiest & cleanest way for you would be a "List Comprehension", see here.

Here is the respective code:

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]      
var = [(i) for i in friends]      

Should do what you want, right?

Irgendniemand
  • 184
  • 1
  • 9
0

If you want to join the values in friends into a comma-separated string, that would be

s = ','.join(friends)

If you want to include quotes around the names, maybe something like

s = ','.join(['"{0}"'.format(x) for x in friends])
tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Try this at the end of the loop:

the_variable = the_variable + i

However, if you are to do this, you should add a space to the end of every item in the dictionary, otherwise it will output:

JoeZoeBradAngelinaZukiThandiParis
CheezBiscuit
  • 50
  • 1
  • 9
0

I would use a dictionary instead, as I too spent a while looking into this, and determined that a dictionary would be easy enough.

    friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
    dict= {}
    for i in friends:
       dict[i] = i

    print(dict)
    print(dict['Zuki'])
    dict['Zuki'] = "Tim Smith"

    print(dict['Zuki'])

The Other option would be to just call the number:

    print(friends[0])

As for automatic assignment I haven't found a way to do it.

Dayantat
  • 108
  • 1
  • 6